我正在将 JSON 数据从 jQuery 传递到我的 Java Controller ,并且我正在使用 @RequestBody
,但我收到一个异常:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
我传递的数据是:
myData = {
"source": "CSS",
"type": "CSS2",
"typeValue": "value",
"textarea_value": " desc"
}:
我用来传递此数据的 AJAX 调用是:
$.ajax({
url: './common/deleteData',
type: 'POST',
data: myData,
success: function(data) {
alert("Successfully Deleted Source..");
},
error: function(data) {}
});
我的Java Controller 如下
@RequestMapping(value = "/common/deleteData", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded; charset=UTF-8"})
public String deleteData(@RequestBody SourceDelete sourcedelete, final HttpServletRequest request, final RedirectAttributes rdtAttribs) throws ApplicationException
{
LOGGER.entry("Deleting the Merge Preference Details");
System.out.println(sourcedelete.getSource());
return null;
}
我的 POJO 对象如下:
public class SourceDelete {
private String source;
private String type;
private String typeValue;
private String textarea_value;
//Setters and Getters
}
有人可以帮我弄清楚为什么会出现此错误以及应该如何修复它。
最佳答案
删除@RequestBody注释,
@RequestMapping(value = "/common/deleteData", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded; charset=UTF-8"})
public String deleteData(SourceDelete sourcedelete, final HttpServletRequest request, final RedirectAttributes rdtAttribs) throws ApplicationException
{
LOGGER.entry("Deleting the Merge Preference Details");
System.out.println(sourcedelete.getSource());
return null;
}
关于java - 将 JSON 数据从 JQUERY 传递到 java Controller 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58093668/