<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootJsonDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.example.domian;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CurrentUserDTO implements Serializable {
private String name;
private String avatar;
@JSONField(name = "userid")
private String userId;
private String email;
private String signature;
private String title;
@JSONField(name = "group")
private String groupInfo;
private Integer notifyCount;
private Integer unreadCount;
private String country;
private GeographicBean geographic;
private String address;
private String phone;
private List<TagsBean> tags;
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class GeographicBean {
private ProvinceBean province;
private CityBean city;
@Data
public static class ProvinceBean {
private String label;
private String key;
}
@Data
public static class CityBean {
private String label;
private String key;
}
}
@Data
public static class TagsBean {
private String key;
private String label;
}
}
package com.example.controller;
import com.example.domian.CurrentUserDTO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class SpringBootJsonController {
@GetMapping("/showJson")
public CurrentUserDTO showJson() {
CurrentUserDTO currentUserDTO = null;
try {
currentUserDTO = (CurrentUserDTO) Class.forName ("com.example.domian.CurrentUserDTO").newInstance ();
currentUserDTO.setName ("小米");
currentUserDTO.setEmail ("9527@qq.com");
List<CurrentUserDTO.TagsBean> tagsBeans = new ArrayList<> ();
for (int i = 0; i < 5; i++) {
CurrentUserDTO.TagsBean tagsBean = new CurrentUserDTO.TagsBean ();
tagsBean.setKey (i+"");
tagsBean.setLabel ("很有想法");
tagsBeans.add (tagsBean);
}
currentUserDTO.setTags (tagsBeans);
CurrentUserDTO.GeographicBean.ProvinceBean provinceBean = new CurrentUserDTO.GeographicBean.ProvinceBean ();
provinceBean.setKey ("33000");
provinceBean.setLabel ("浙江省");
CurrentUserDTO.GeographicBean.CityBean cityBean = new CurrentUserDTO.GeographicBean.CityBean ();
cityBean.setKey ("杭州市");
cityBean.setLabel ("330100");
currentUserDTO.setGeographic (new CurrentUserDTO.GeographicBean (provinceBean,cityBean));
} catch (Exception e) {
e.printStackTrace ();
}
return currentUserDTO;
}
}
结果

の随便写写