[英]@RequestBody is always empty in Spring
The JSONObject is always coming as empty for the method below. 对于以下方法,JSONObject始终为空。
@RequestMapping(value = "/package/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public SPackage updatePackage(@PathVariable String id, @RequestBody JSONObject
sPackage) {
}
and my ajax is like this. 我的ajax就是这样 I am ways getting the object as empty map on the server side
我正在将对象作为服务器端的空映射
var jsonObject= {"customerName":$('#customerName').val()}
var jsonData = JSON.stringify(jsonObject);
$.ajax({
type: "PUT",
url: "http://localhost:8081/someproj/package/" + $('#id').val(),
dataType: "json",
data: jsonData,
async: false,
contentType: "application/json; charset=utf-8",
beforeSend : function() {
openModal();
},
success: function(data) {
closeModal();
$('#success').show();
console.log(data);
}
});
I guess spring
doesn't know to convert your json
to JSONObject
, the best thing would be to accept a POJO object
which has similar structure to your json
, 我想
spring
不知道将json
转换成JSONObject
,最好的办法是接受一个结构类似于json
的POJO object
,
@RequestMapping(value = "/package/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public SPackage updatePackage(@PathVariable String id, @RequestBody YourJsonPOJO
sPackage) {
}
Are you sure there're no exceptions occurring in your Spring code. 您确定Spring代码中没有异常发生。 When converting from JSON to custom object in Spring, you need to specify a custom class that has same fields & format as the JSON coming in. Otherwise, Spring doesn't know who to convert HTTP POST data into a Java object.
在Spring中从JSON转换为自定义对象时,您需要指定一个自定义类,该类具有与传入的JSON相同的字段和格式。否则,Spring不知道是谁将HTTP POST数据转换为Java对象。
In your case, you could do define a POJO like this: 在您的情况下,可以这样定义POJO:
public class MyRequestObj {
private String customerName;
// add more fields for any other keys from JSON
}
And put this in your Controller class: 并将其放在您的Controller类中:
@RequestMapping(value = "/package/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public SPackage updatePackage(@PathVariable String id, @RequestBody MyRequestObj
myRequestObj) {
String customerName = myRequestObj.getCustomerName();
}
Of course, if you only want to pass in the customer name as a String to your Controller, then you could also pass it as query string (append ?customerName=someCustomer) and you can retrieve it in Spring as: 当然,如果只想将客户名称作为字符串传递给Controller,则也可以将其作为查询字符串传递(追加?customerName = someCustomer),并且可以在Spring中将其检索为:
@RequestMapping(value = "/package/{id}", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public SPackage updatePackage(@PathVariable String id, @RequestParam("customerName") String customerName) {
}
You can use this workaround: 您可以使用以下解决方法:
@RequestBody Map<String, String> json
That way you can continue using Jackson HttpMessageConverter and work with custom objects in payload. 这样,您可以继续使用Jackson HttpMessageConverter并在有效负载中使用自定义对象。
You can check extended explanaition why this happens at answer here 您可以在此处的答案中查看扩展解释,为什么会发生这种情况
@RequestBody gives empty JsonObject when making a POST Request 发出POST请求时,@ RequestBody给出空的JsonObject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.