@JsonProperty的使用

内容分享1周前发布
0 0 0

@JsonProperty的使用

<!--maven依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>

  1. @JsonProperty注解主要用于实体类的属性上,作用可以简单的理解为在反序列化的时候给属性重命名(多一个名字来识别)
  2. 代码示例
  • 实体类

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
/**
 * @Author: Juncheng
 * @Date: 2021/5/18 16:45
 * @Description:
 */
@Data
public class TestJson {
    @JsonProperty("my_name")
    private String myName;
    @JsonProperty("my_age")
    private Integer myAge;
}

  • controller

import com.example.demojson.TestJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Juncheng
 * @Date: 2021/5/18 16:45
 * @Description:
 */
@RestController
@RequestMapping("/test")
@Api("swagger文档")
public class TestController {
    @PostMapping("/json")
    @ApiOperation("测试@JsonProperty")
    public TestJson testJson(@RequestBody TestJson testJson) {
        String myName = testJson.getMyName();
        Integer myAge = testJson.getMyAge();
        System.out.println(testJson.toString());
        return testJson;
    }
}

  • 接口请求参数

    @JsonProperty的使用

  • 接口返回值

    @JsonProperty的使用

  • 日志打印的值

    @JsonProperty的使用

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
none
暂无评论...