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 访问

SpringBoot_2_返回json格式_第1张图片
Paste_Image.png

你可能感兴趣的:(SpringBoot_2_返回json格式)