fil*_*zyk 3 java spring json jackson spring-boot
您好,我想知道在使用Spring Boot时如何将json消息映射到java中的对象。
假设我正在像
{
"customerId": 2,
"firstName": "Jan",
"lastName": "Nowak",
"town": "Katowice"
}
Run Code Online (Sandbox Code Playgroud)
并且我想在我的java程序中使其成为实体:出于任何原因,我都不 希望字段名匹配
public class Customer {
//Something like @Map("customerId")
private long OMG;
//Something like @Map("firstName")
private String WTF;
//Something like @Map("lastName")
private String LOL;
//Something like @Map("town")
private String YOLO;
Run Code Online (Sandbox Code Playgroud)
我找不到应该使用的注释,而不是仅使用spring boot转换器内置的jackson?
Spring Boot随带现成的Jackson。
您可以使用@RequestBody Spring MVC批注将json字符串解组到Java对象中。
Run Code Online (Sandbox Code Playgroud)@RestController public class CustomerController { //@Autowired CustomerService customerService; @RequestMapping(path="/customers", method= RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public Customer postCustomer(@RequestBody Customer customer){ //return customerService.createCustomer(customer); } }
使用@JsonProperty和相应的json字段名称注释您的实体成员元素。
Run Code Online (Sandbox Code Playgroud)public class Customer { @JsonProperty("customerId") private long OMG; @JsonProperty("firstName") private String WTF; @JsonProperty("lastName") private String LOL; @JsonProperty("town") private String YOLO; }
| 归档时间: |
|
| 查看次数: |
2704 次 |
| 最近记录: |