首页 > 其他分享 >注解@JSONField和@JsonProperty的简单使用

注解@JSONField和@JsonProperty的简单使用

时间:2022-11-01 00:33:19浏览次数:42  
标签:JsonProperty java org springframework private historyPasswordDemo 注解 import JSON

本注解的使用,需要引入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

相关文章

  • 自定义注解+AOP实现参数校验
    这边是在学习了AOP和自定义注解之后,就想着将他们两个整合起来,以自定义注解进行标注,以AOP的反射获取信息,然后对代码进行加强,所以这边就简单的实现了一个进行邮箱参数格式校......
  • Spring-8-注解(对象的创建)
    文章目录​​注解的di​​​​1.使用注解第一步加入maven的依赖(spring-context)​​​​2.在类里面加spring的注解​​​​创建对象1.@Context(value="对象名")==@Context("对......
  • Java通过注解运行方法
    //上下文@ResourceprivateApplicationContextapplicationContext;@Beanpublicvoidtest(){//扫描ControllerReflectionsreflections=newReflection......
  • 常用注解
    ##注解说明-@Autowired:自动装配通过类型,名字-@nullable:字段标记了这个注解,说明这个字段可以为null。-@Resource:自动装配通过名字,类型-@Component:组件,放在类上,说......
  • Lombok不常见但实用的注解
    目录@SneakyThrows@UtilityClass@CleanUp@AllArgsConstructor资料参考地址1:@SneakyThrows注解资料参考地址2:Lombok之@Cleanup使用资料参考地址3:lombok实验性注解......
  • Spring注解之@Value基于Apollo或者YAML文件为静态变量赋值
    摘要:SpringBoot微服务中,把在Apollo配置中心或者YAML文件里配置的属性赋值给静态变量。综述  Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同......
  • Java注解
    1.Java注解的描述java注解又叫做java标注,是java的一种注释机制,在jdk5.0引入。其可以作用在类、方法、变量、参数和包上。另外,其可以通过反射来获取注解标注的内容。可以说......
  • spring注解--切面(AOP)相关注解
    切面(AOP)相关注解Spring支持AspectJ的注解式切面编程。@Aspect声明一个切面(类上)使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点)作为参数。@After在......
  • spring注解--@Bean的属性支持
    @Bean的属性支持@Scope设置Spring容器如何新建Bean实例(方法上,得有@Bean)其设置类型包括:·Singleton(单例,一个Spring容器中只有一个bean实例,默认模式),·Protetype(每......
  • spring注解--声明注解
    1.声明bean的注解@Component 组件,通用的注解方式@Component作用:调用无参构造创建一个bean对象,并把对象存入spring的IOC容器,交由spring容器进行管理。相当于在xml中配......