[英]JSON post to Spring Controller
嗨,我從Spring的Web Services開始,所以我試圖在Spring + JSON + Hibernate中開發小型應用程序。 我對HTTP-POST有問題。 我創建了一個方法:
@RequestMapping(value="/workers/addNewWorker", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public String addNewWorker(@RequestBody Test test) throws Exception {
String name = test.name;
return name;
}
我的模型測試看起來像:
public class Test implements Serializable {
private static final long serialVersionUID = -1764970284520387975L;
public String name;
public Test() {
}
}
通過POSTMAN,我僅發送JSON {“ name”:“ testName”},但我總是收到錯誤;
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
我導入了傑克遜圖書館。 我的GET方法工作正常。 我不知道我在做什么錯。 我很感謝任何建議。
使用將您的JSON對象轉換為JSON字符串
JSON.stringify({“ name”:“ testName”})
或手動。 @RequestBody需要json字符串而不是json對象。
注意:stringify函數在某些IE版本中存在問題,firefox可以使用
驗證POST請求的ajax請求的語法。 Ajax請求中需要processData:false屬性
$.ajax({
url:urlName,
type:"POST",
contentType: "application/json; charset=utf-8",
data: jsonString, //Stringified Json Object
async: false, //Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation
cache: false, //This will force requested pages not to be cached by the browser
processData:false, //To avoid making query String instead of JSON
success: function(resposeJsonObject){
// Success Action
}
});
控制者
@RequestMapping(value = urlPattern , method = RequestMethod.POST)
public @ResponseBody Test addNewWorker(@RequestBody Test jsonString) {
//do business logic
return test;
}
@RequestBody
RequestBody-將Json對象轉換為Java
@ResponseBody
將Java對象轉換為json
您需要為模型Test
類中已定義的所有字段包括getter和setter -
public class Test implements Serializable {
private static final long serialVersionUID = -1764970284520387975L;
public String name;
public Test() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
嘗試改用application / *。 並使用JSON.maybeJson()檢查控制器中的數據結構。
如果要將json用作http請求和響應,請執行以下操作。 因此,我們需要在[context] .xml中進行更改
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>
將pingpingJackson2HttpMessageConverter映射到RequestMappingHandlerAdapter messageConverters,以便Jackson API啟動並將JSON轉換為Java Bean,反之亦然。 通過進行此配置,我們將在請求正文中使用JSON,並將在響應中接收JSON數據。
我還為控制器部分提供了一些小代碼段:
@RequestMapping(value = EmpRestURIConstants.DUMMY_EMP, method = RequestMethod.GET)
public @ResponseBody Employee getDummyEmployee() {
logger.info("Start getDummyEmployee");
Employee emp = new Employee();
emp.setId(9999);
emp.setName("Dummy");
emp.setCreatedDate(new Date());
empData.put(9999, emp);
return emp;
}
因此,在上面的代碼中,emp對象將直接轉換為json作為響應。 郵寄也會發生同樣的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.