注解@JSONField和@JsonProperty的简单使用
本注解的使用,需要引入fastjson依赖
1 2 3 4 5 6 | <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 70 </version> </dependency> |
首先对时间格式化
时间格式化后
代码部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.java.test.json.field; import lombok.Data; import java.util.Date; /** * @Description: * @Author: Yourheart * @Create: 2022/10/31 13:01 */ @Data public class HistoryPasswordDemo { /** * id值 */ private String id; /** * 密码 */ private String pass_word; /** * 创建时间 */ private Date create_time; /** * 修改时间 */ private Date updateTime; /** * 密码创建用户 */ private String create_user; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.java.test.json.field; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.Date; import java.util.List; @Controller @CrossOrigin @RequestMapping ( "/historyPassword" ) @Slf4j public class HistoryPasswordController { /** * * @param historyPasswordDemo * @return */ @RequestMapping ( "/getHistoryPasswordList" ) @ResponseBody public HistoryPasswordDemo getHistoryPasswordList( @RequestBody HistoryPasswordDemo historyPasswordDemo) { log.info( "historyPasswordDemo:{}" , historyPasswordDemo); List<HistoryPasswordDemo> historyPasswordList = new ArrayList<>(); historyPasswordList.add(historyPasswordDemo); historyPasswordDemo.setCreate_time( new Date()); historyPasswordDemo.setUpdateTime( new Date()); log.info( "【时间格式化前】historyPasswordDemo:{}" , historyPasswordDemo); return historyPasswordDemo; } } |
1 2 3 4 | # 格式化全局时间字段 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss # 指定时间区域类型 spring.jackson.time-zone=GMT+ 8 |
@JsonProperty使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.java.test.json.field; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; /** * @Description: * @Author: Yourheart * @Create: 2022/10/27 18:15 */ @Data public class UserDTO { /** * 用户名 */ @JsonProperty ( "user_name" ) private String userName; /** * 密码 */ @JsonProperty ( "pass_word" ) private String password; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.java.test.json.field; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; /** * @Description: * @Author: Yourheart * @Create: 2022/10/27 17:39 */ @RequestMapping ( "/login" ) @RestController @CrossOrigin @Slf4j public class LoginController { @PostMapping ( "/login" ) public Map<String, Object> login( @RequestBody UserDTO userDTO){ log.info( "userDTO:{}" ,userDTO); Map<String,Object> result= new HashMap<>(); if (StringUtils.equals( "admin" ,userDTO.getUserName())&&StringUtils.equals( "123" ,userDTO.getPassword())){ result.put( "code" , "100" ); result.put( "token" ,userDTO.getUserName()+ "" +userDTO.getPassword()); result.put( "message" , "登录成功" ); return result; } result.put( "code" , "-100" ); result.put( "message" , "登录失败" ); return result; } } |
@JSONField使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.java.test.json.field; import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * @Description: * @Author: Yourheart * @Create: 2022/10/31 11:25 */ @Data public class HistoryPassword implements Serializable { private static final long serialVersionUID = 3604972003323896788L; /** * id值 */ @JSONField (name = "id" ) private String id; /** * 密码 */ @JSONField (name = "pass_word" ) private String passWord; /** * 创建时间 */ @JSONField (name = "create_time" ) private Date createTime; /** * 修改时间 */ private Date updateTime; /** * 密码创建用户 */ @JSONField (name = "create_user" ) private String createUser; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.java.test.json.field; import lombok.Data; import java.util.Date; /** * @Description: * @Author: Yourheart * @Create: 2022/10/31 13:01 */ @Data public class HistoryPasswordDemo { /** * id值 */ private String id; /** * 密码 */ private String pass_word; /** * 创建时间 */ private Date create_time; /** * 修改时间 */ private Date updateTime; /** * 密码创建用户 */ private String create_user; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.java.test.json.field; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.List; @Controller @CrossOrigin @RequestMapping ( "/historyPassword" ) @Slf4j public class HistoryPasswordController { /** * * @param historyPasswordDemo * @return */ @RequestMapping ( "/getHistoryPasswordList" ) @ResponseBody public HistoryPasswordDemo getHistoryPasswordList( @RequestBody HistoryPasswordDemo historyPasswordDemo) { log.info( "historyPasswordDemo:{}" , historyPasswordDemo); List<HistoryPasswordDemo> historyPasswordList = new ArrayList<>(); historyPasswordList.add(historyPasswordDemo); return historyPasswordDemo; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package com.java.test; import com.alibaba.fastjson.JSONObject; import com.java.test.json.field.HistoryPassword; import com.java.test.json.field.HistoryPasswordDemo; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.springframework.web.client.RestTemplate; import java.util.Date; /** * @Description: * @Author: Yourheart * @Create: 2022/10/31 13:02 */ @Slf4j public class JsonFieldTest { @Test public void test(){ RestTemplate restTemplate= new RestTemplate(); String url = "http://127.0.0.1:2001/historyPassword/getHistoryPasswordList" ; HistoryPasswordDemo historyPasswordDemo = new HistoryPasswordDemo(); historyPasswordDemo.setId( "123" ); historyPasswordDemo.setPass_word( "qwe123" ); historyPasswordDemo.setCreate_time( new Date()); historyPasswordDemo.setUpdateTime( new Date()); historyPasswordDemo.setCreate_user( "admin" ); String postForObject = restTemplate.postForObject(url, historyPasswordDemo, String. class ); HistoryPassword historyPassword = JSONObject.parseObject(postForObject, HistoryPassword. class ); log.info( "historyPassword:{}" ,historyPassword); } } |
完整pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.java</groupId> <artifactId>test-study</artifactId> <version> 1.0 -SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.2 . 1 .RELEASE</version> <relativePath/> </parent> <dependencies> <!--tomcat容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 16 </version> </dependency> <!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12 </version> </dependency> <!--判断空的用法 --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version> 2.6 </version> </dependency> <!-- https: //mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version> 12.2 . 0.1 </version> </dependency> <!--springboot整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version> 2.1 . 2 </version> </dependency> <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version> 1.2 . 70 </version> </dependency> <!-- 热部署模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional> true </optional> <!-- 这个需要为 true 热部署才有效 --> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>study</finalName> </build> </project> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异