一、说明

SpringBoot框架已经自动封装好json字符串解析,所以我们只需要用它的注解来返回操作就可以了.

二、实战

1、书写一个实体类User,设置属性id和name

 package com.json;

 /**
* Created by liuya on 2018-01-17.
*/
public class UserPoJo
{
private int userId;
private String userName; public int getUserId() {
return userId;
} public void setUserId(int userId) {
this.userId = userId;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} @Override
public String toString() {
return "UserPoJo{" +
"userId=" + userId +
", userName='" + userName + '\'' +
'}';
}
}

2、书写开启服务器的代码(SpringBoot框架自带一个测试服务器)

 package com.json;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringboothelloApplication { public static void main(String[] args) {
SpringApplication.run(SpringboothelloApplication.class, args);
} }

3、书写controller返回json的代码

 package com.json;

 import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* Created by liuya on 2018-01-16.
*
* 测试用的一个例子
*/ @RestController
public class ControllerJson { @RequestMapping("user")
public UserPoJo hello(){
//实体类赋值
UserPoJo userPoJo = new UserPoJo();
userPoJo.setUserId(111);
userPoJo.setUserName("王小二");
//返回实体类
return userPoJo;
}
}

4、启动服务器的代码,生成如图

5、浏览器测试

IntelliJ IDEA 2017版 SpringBoot的Json字符串返回的更多相关文章

  1. IntelliJ IDEA 2017版 spring-boot 2.0.3 部署war包项目和jar包项目

    1.建立项目 Java Controller package com.springboot.jsp.controller; import org.springframework.stereotype. ...

  2. IntelliJ IDEA 2017版 spring-boot与Mybatis简单整合

    一.编译器建立项目 参考:http://www.cnblogs.com/liuyangfirst/p/8372291.html 二.代码编辑 1.建立数据库 /* Navicat MySQL Data ...

  3. IntelliJ IDEA 2017版 spring-boot加载jsp配置详解(详细图文实例)

    一.创建项目 (File--->New-->Project) 2.项目配置内容 3.选择配置项目的Group包名,Artifact项目名称 4.选择项目类型为web类型 5.创建成功,点击 ...

  4. IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)

    一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...

  5. IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (二)

    第二部分 邮件发送历史   一.第一封邮件   1.1969年10月,世界上的第一封电子邮件    1969年10月世界上的第一封电子邮件是由计算机科学家Leonard K.教授发给他的同事的一条简短 ...

  6. IntelliJ IDEA 2017版 spring-boot 2.03 去除控制台logo;去除springboot 图标;去除springboot 图

    1.控制台出现logo图标 2.如果不喜欢,如何去除这个logo 在其Application中进行设置 package com.springboot.logo; import org.springfr ...

  7. IntelliJ IDEA 2017版 SpringBoot测试类编写

    SpringBoot的测试类编写Demo 源码见 https://github.com/liushaoye/baseone.git

  8. IntelliJ IDEA 2017版 SpringBoot的关闭自动配置和自定义Banner

    一.关闭自动配置 在jar包下找下边的名字    设置关闭自动配置jar    多个的时候配置       二.自定义Banner   (1)网站搜索一个图案.网址:http://patorjk.co ...

  9. IntelliJ IDEA 2017版 SpringBoot的web项目补充

    一.注解        @SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置.        @Configuration:这是一个配置Sprin ...

随机推荐

  1. Reusing & Composing GraphQL APIs with GraphQL Bindings

    With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...

  2. hasura graphql 引擎基本试用

    hasura 使用一个基于pg数据库的graphql引擎,他的设计比postgrpahql 有好多方便的地方,同时使用也比较简单 安装 docker && docker-compose ...

  3. Fortify SCA 分析代码漏洞全解

    上次介绍了用FindBugs辅助分析代码漏洞.这次换了一个工具:Fortify SCA Demo 4.0.0.Fortify是一个在安全方面挺出名的公司,这里就不多说了.先介绍一下主角:Fortify ...

  4. centos下memcached安装

    memcached是一款高速.分布式的内存缓存系统.其官方主页在http://www.danga.com/memcached/ 1.安装前的准备 要安装memcached,需要有libevent的支持 ...

  5. .NET泛型解析(上)

    [1]:泛型介绍 泛型是C#2.0中一个重要的新特性,泛型是CLR和编程语言提供的一种特殊机制,它支持另一种形式的代码重用.泛型通常用与集合以及作用于集合的方法一起使用,当然也可以单独使用. C#是一 ...

  6. Python的学习计划

    整体进度(6-7个月毕业)一.(2月左右)Python基础二.数据库(1-2周)---存储数据和信息(本质上和文件没有区别) 增删改查更方便了三.前端(2周左右)---html.css等等四.框架(2 ...

  7. PHP代码实现 1

    $PHP-SRC/run-test.php 因为如果在同一个进程中执行, 测试就会停止,后面的测试也将无法执行,php中有很多将脚本隔离的方法比如: system(),exec()等函数,这样可以使用 ...

  8. 负载均衡-haproxy安装配置

    HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持 ...

  9. linux编程基本

    库的使用头文件:.h 里面的函数及变量的声明 比如#include <stdio.h> ,Linux下默认头文件的搜索路径 系统定义的头文件: /usr/include /usr/loca ...

  10. EasyUI使用小常识

    datagrid:1 //显示某列 $('#ListTable').datagrid('showColumn', 'ExRate'); //隐藏某列 $('#ListTable').datagrid( ...