<!--maven依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
- @JsonProperty注解主要用于实体类的属性上,作用可以简单的理解为在反序列化的时候给属性重命名(多一个名字来识别)
- 代码示例
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;
}
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;
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。