Reputation: 3373
I am using Spring 3.2 and try to use an ajax post request to submit an array of json objects. If this is relevant, I escaped all special characters.
I am getting an HTTP Status of 415.
My controller is:
@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json")
public @ResponseBody String saveProfileJson(@RequestBody String[] profileCheckedValues){
System.out.println(profileCheckedValues.length);
return "success";
}
jquery is:
jQuery("#save").click(function () {
var profileCheckedValues = [];
jQuery.each(jQuery(".jsonCheck:checked"), function () {
profileCheckedValues.push($(this).val());
});
if (profileCheckedValues.length != 0) {
jQuery("body").addClass("loading");
jQuery.ajax({
type: "POST",
contentType: "application/json",
url: contextPath + "/sample/save-profile",
data: "profileCheckedValues="+escape(profileCheckedValues),
dataType: 'json',
timeout: 600000,
success: function (data) {
jQuery('body').removeClass("loading");
},
error: function (e) {
console.log("ERROR: ", e);
jQuery('body').removeClass("loading");
}
});
}
});
and an example of one of the objects from the array I am posting is the following json:
{
"id": "534213341",
"name": "Jack Lindamood",
"first_name": "Jack",
"last_name": "Lindamood",
"link": "https://www.facebook.com/jack",
"username": "jack",
"gender": "male",
"locale": "en_US",
"updated_time": "2013-07-23T21:13:23+0000"
}
The error is:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method
Why is this error happening - does anyone know?
Upvotes: 15
Views: 64585
Reputation: 1
I had the same issue and everything looked ok. Other requests working like a charm in the same project. Finally it turned out out that my request object had a duplicate the:
@JsonProperty("duplicatePropertyName")
entry. The error message:
415 Unsupported Media Type
Is really confusing in this case. Hope this helps to people with bad eyes like me ;-)
Upvotes: 0
Reputation: 99
I had the same problem, I just brought up the below line of code in the spring-servlet.xml, it was at the almost end of this xml file. It worked.
<mvc:annotation-driven />
Upvotes: 0
Reputation: 196
1) Add the following dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version> // 2.4.3
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version> // 2.4.3
</dependency>
2) If you are using @RequestBody annotation in controller method make sure you have added following in xml file
<mvc:annotation-driven />
This should resolve the 415 status code issue.
Upvotes: 6
Reputation: 17345
I had consume="application/json"
in my request mapping configuration which expects a json input. When I sent a request which is not a JSON input Spring complained about the 415 Unsupported Media Type. So removing the consume="application/json"
worked for me. So look into the request input and the accept media type in your Spring controller method signature. A mismatch would throw the 415 error. This will be most likely the reason for the issue.
Upvotes: 2
Reputation: 4684
I had the same problem. I had to follow these steps to resolve the issue:
1. Make sure you have the following dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version> // 2.4.3
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version> // 2.4.3
</dependency>
2. Create the following filter:
public class CORSFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
String origin = request.getHeader("origin");
origin = (origin == null || origin.equals("")) ? "null" : origin;
response.addHeader("Access-Control-Allow-Origin", origin);
response.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, DELETE, OPTIONS");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Access-Control-Allow-Headers",
"Authorization, origin, content-type, accept, x-requested-with");
filterChain.doFilter(request, response);
}
}
3. Apply the above filter for the requests in web.xml
<filter>
<filter-name>corsFilter</filter-name>
<filter-class>com.your.package.CORSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>corsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
I hope this is useful to somebody.
Upvotes: 0
Reputation: 5461
You may try with HttpServletRequest
. it does not have any problem
@RequestMapping(value = "/save-profile", method = RequestMethod.POST,consumes="application/json",headers = "content-type=application/x-www-form-urlencoded")
public @ResponseBody String saveProfileJson(HttpServletRequest request){
System.out.println(request.getParameter("profileCheckedValues"));
return "success";
}
Upvotes: 7
Reputation: 17361
You will need to add jackson and jackson-databind to the classpath. Spring will pick it up using it's MappingJacksonHttpMessageConverter
An HttpMessageConverter implementation that can read and write JSON using Jackson's ObjectMapper. JSON mapping can be customized as needed through the use of Jackson's provided annotations. When further control is needed, a custom ObjectMapper can be injected through the ObjectMapper property for cases where custom JSON serializers/deserializers need to be provided for specific types. By default this converter supports (application/json).
Upvotes: 3
Reputation: 47290
try changing json
to application/json
for datatype. And what does your json look like is it valid ? I have never seen json converted to a String[] before, so this coul dalso be the problem.
Upvotes: 0