SpringBoot_2_返回json格式

1 启动类

@RestController
@SpringBootApplication
public class AppRun {
    public static void main(String[] args){
        SpringApplication.run(AppRun.class, args);
    }
}

2 Model类

import java.util.Date;
public class StudentModel {
    private Integer id;
    private String  name;
    private Date    birth;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
}

3 控制类

import java.util.Date;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/student")
public class StudentController {
    @RequestMapping("/getStudent")
    public StudentModel getStudent(){
        StudentModel student = new StudentModel();
        student.setId(1);
        student.setName("Jason");
        student.setBirth(new Date());
        return student;
    }
}

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

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • spring官方文档:http://docs.spring.io/spring/docs/current/spri...
    牛马风情阅读 1,732评论 0 3
  • 自控力不强,拖延症很严重,什么时候才能成自己为之自豪的人?
    e7e912977a84阅读 244评论 0 0
  • 还有一点不对劲 却已来不及思考 你的温柔,让我心碎 你的真心,将我困牢 遇见你,我在劫难逃 还有一点不对劲 却已经...
    作家明至阅读 593评论 3 3