對象我有SpringBoot應用程序與依賴關係:春天開機自動JSON在控制器
<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>
我有我的控制器的方法如下:
@RequestMapping(value = "/liamo", method = RequestMethod.POST)
@ResponseBody
public XResponse liamo(XRequest xRequest) {
...
return something;
}
我從送一個JSON對象我通過AJAX與一些XRequest類型對象的字段(這是一個普通的POJO沒有任何註釋)的HTML。然而,我的控制器方法沒有將我的JSON構造成對象,並且它的字段爲空。
我想在我的控制器上進行自動反序列化?
可能是'@ RequestBody'註釋你的'xRequest'參數:http://stackoverflow.com/questions/11291933/requestbody-and-responsebody-annotations-in-spring - 順便說一句,如果您使用spring-mvc作爲控制器,則不需要'spring-boot-starter-jersey'。 – zapl
您缺少'@ RequestBody'像@zapl注意到的那樣,如果您要發送JSON,我還會將'consumes =「application/json」'添加到'@ RequestMapping'。 – VladoDemcak
答案就是這樣。缺少RequestBody,謝謝!你能把它寫成接受它的答案嗎? – kamaci