I am trying to pass simple JSON object to Spring MVC controller and getting error as “NetworkError: 415 Unsupported Media Type - https://my_url.html”
I am using Spring 3.2.10, jquery 1.6 and com.googlecode.json-simple 1.1.1 libraries. Follow I post my code,
JSP
function myTestFunction(year){
$.ajax({
type: "POST",
url: "my_url.html",
data: "{\"testName\": \"MyName\"}",
contentType: "application/json",
mimeType: "application/json",
success: function(response){
console.log("SUCCESS: " + response);
},
error: function (e) {
console.log("Error " + e);
}
});
Controller class
@RequestMapping(value = "/my_url.html", method = RequestMethod.POST)
public void myTestMethod(@RequestBody MyInfo myInfo, HttpServletRequest request, HttpServletResponse response) throws Exception{
// my code
}
MyInfo class
public class MyInfo {
private String testName;
public MyInfo () {
}
public MyInfo (String testName) {
this.testName = testName;
}
public String getTestName() {
return testName;
}
public void setTestName(String testName) {
this.testName = testName;
}
}
I have tried with several options by adding dataType: 'json' and sending object using JSON.stringify But didn't work for me.
And also I already put the “<mvc:annotation-driven />” tag in my spring configuration file. What am I miss or doing incorrectly ?