spring boot起步之返回json数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuhuanchao/article/details/73012597

在做如下操作之前,我们对之前的Hello进行简单的修改,我们新建一个包com.dinglit 然后新建一个类HelloControoler, 然后修改App.java类,主要是的这个类就是一个单纯的启动类。

package com.dinglit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 169876809
 * 
 * @author qianzhang 
 * 
 * 2017-06-11 7:45:34
 * 
 * 其中@SpringBootApplication申明让spring boot自动给程序进行必要的配置,
 * 等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan
 * 
 * 要将Application放在最外层,也就是要包含所有子包。
 */
//@RestController
@SpringBootApplication
public class App {

	/*
	 * @RequestMapping("/") public String hello(){ return "Hello world!"; }
	 */
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
HelloController类代码如下:

package com.dinglit.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 169876809
 * 
 * @author qianzhang 
 * 
 * 2017-06-11 7:55:34
 * 
 */

@RestController// 标记为:restful
public class HelloController {
   
	/**
	 * 返回demo数据: 请求地址:http://127.0.0.1:8080/
	 *  
	 * @return
	 */
	
    @RequestMapping("/")
    public String hello(){
       return"Hello world!";
    }
}
我们在编写接口的时候,时常会有需求返回json数据,那么在spring boot应该怎么操作呢?主要是在class中加入注解@RestController,。
返回JSON之步骤:
(1)编写一个实体类UserDO
(2)编写UserController;
(3)在UserController加上@RestController和@RequestMapping注解;
(4)测试;

UserController代码如下:

package com.dinglit.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dinglit.domian.UserDO;

/**
 * 169876809
 * 
 * @author qianzhang 
 * 
 * 2017-06-11 8:45:34
 * 
 */

@RestController
@RequestMapping("/user")
public class UserController {

	/**
	 * 返回demo数据: 请求地址:http://127.0.0.1:8080/user/getUser
	 * 
	 * @return
	 */
	@RequestMapping("/getUser")
	public UserDO getUserDO() {
		UserDO userDO = new UserDO();
		userDO.setId(1);
		userDO.setName("qianzhang");
		userDO.setPassword("hhahahahaha");
		return userDO;
	}

}
UserDO的代码如下:
package com.dinglit.domian;

/**
 * 169876809
 * 
 * @author qianzhang 
 * 
 * 2017-06-11 8:40:34
 * 
 */
public class UserDO {

	private int id;

	private String name;

	private String password;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
运行结果如图:

{"id":1,"name":"qianzhang","password":"hhahahahaha"}


总结:

Spring Boot也是引用了JSON解析包Jackson,那么自然我们就可以在Demo对象上使用Jackson提供的json属性的注解,对时间进行格式化,对一些字段进行忽略等等。




本系列所有代码已同步到 GitHub, 项目地址 springboot-study,欢迎 star ,如有问题,还请留言。


如果觉得我的文章或者代码对您有帮助,可以微信打赏请我喝杯咖啡。 
您的支持将鼓励我继续创作!谢谢! 



猜你喜欢

转载自blog.csdn.net/liuhuanchao/article/details/73012597
今日推荐