本注解的使用,需要引入fastjson依赖
<!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency>
首先对时间格式化
时间格式化后
代码部分
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; }
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; } }
# 格式化全局时间字段 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss # 指定时间区域类型 spring.jackson.time-zone=GMT+8
@JsonProperty使用
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; }
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使用
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; }
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; }
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; } }
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
<?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>
标签:JsonProperty,java,org,springframework,private,historyPasswordDemo,注解,import,JSON From: https://www.cnblogs.com/q202105271618/p/16475418.html