json比较可读,通过键值对返回。实现通常有两种方式:一种是自己来构造,也就是用一个对象存储数据,在最后输出时将其json字符串化;第二种是使用 @RestController 注解实现json数据返回。
导入依赖坐标:
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-jsonartifactId>
- dependency>
实体类:
- public class Pet {
- private Long id;
- private String name;
- private int age;
- private String color;
- private String description;
-
- public Pet(Long id, String name, int age, String color, String description) {
- this.id = id;
- this.name = name;
- this.age = age;
- this.color = color;
- this.description = description;
- }
-
- public Long getId() {
- return id;
- }
-
- public void setId(Long id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public String getColor() {
- return color;
- }
-
- public void setColor(String color) {
- this.color = color;
- }
-
- public String getDescription() {
- return description;
- }
-
- public void setDescription(String description) {
- this.description = description;
- }
- }
创建一个Controller进行测试:
- @RestController
- @RequestMapping("/test_json")
- public class TestJson {
- @Autowired
- public Testjson testjson;
- @RequestMapping("/pet")
- public Pet getPet(){
- return new Pet(1L,"团团",2,"black","a cut panda");
- }
- @RequestMapping("/petList")
- public List
getPetList(){ - List
pets = new ArrayList<>(); - pets.add(new Pet(2L,"小狗",3,"write","a dog"));
- pets.add(new Pet(1L,"小猪",2,"pink","a pig"));
- return pets;
- }
-
- }
完成。
这里使用第三方代替实现,此方法比较粗糙不推荐使用,例如选择 alibaba 开源的 faskjson。依赖配置:
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>2.0.25version>
测试提供的实体类:
- package org.example.pojo;
-
- public class User {
- private String name;
- private String password;
-
- public User(String name, String password) {
- this.name = name;
- this.password = password;
- }
-
- 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;
- }
- }
测试类:
- package org.example.service;
-
- import com.alibaba.fastjson.JSON;
- import org.example.pojo.User;
- import org.springframework.context.annotation.Bean;
- import org.springframework.stereotype.Component;
-
- import java.util.ArrayList;
- import java.util.List;
- @Component
- public class Testjson {
- public String objectToJson(){
- //单个Java 对象
- User user = new User("tfboys","2333");
- String userJsonStr = JSON.toJSONString(user);
- System.out.println("java类转json字符串为:"+userJsonStr);
- //多个java 对象
- User user1 = new User("gameboy","2334");
- User user2 = new User("steatboy","456789");
- List
users = new ArrayList<>(); - users.add(user1);
- users.add(user2);
- String ListUserJson = JSON.toJSONString(users);
- System.out.println("List+ListUserJson);
- jsonToObject();
- return ListUserJson.toString();
- }
- public void jsonToObject(){
- String jsonStr1 = "{'password':'123456','name':'dmeget'}";
- User user = JSON.parseObject(jsonStr1, User.class);
- System.out.println("json字符串转简单java对象:"+user.toString());
- }
- }
这里使用了两个比较重要的方法:一个是把对象json化的toJsonString方法,另一个是把json对象化的parseObject。