<!--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;
}
}