kam*_*aci 10 java json spring-mvc jackson spring-boot
我有SpringBoot应用程序与该依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我在我的控制器上有一个方法如下:
@RequestMapping(value = "/liamo", method = RequestMethod.POST)
@ResponseBody
public XResponse liamo(XRequest xRequest) {
...
return something;
}
Run Code Online (Sandbox Code Playgroud)
我通过AJAX从我的HTML发送一个JSON对象,其中包含一些XRequest类型对象的字段(它是一个没有任何注释的普通POJO).但是我的JSON没有在我的控制器方法中构造成对象,并且它的字段为空.
我错过了我的控制器的自动反序列化?
so-*_*ude 16
Spring启动时带有弹出启动功能,它将处理Java对象的非编组JSON请求主体
您可以使用@RequestBody Spring MVC注释来反序列化/取消编组JSON字符串到Java对象...例如.
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
使用@JsonProperty使用相应的json字段名称注释实体成员元素.
public class Customer {
@JsonProperty("customer_id")
private long customerId;
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("town")
private String town;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34217 次 |
| 最近记录: |