Reputation: 844
I am working on a Spring 4.2 MVC application. Here I am trying to invoke a .html extention url that is supposed to return json data. The problem is that I am getting a org.springframework.web.HttpMediaTypeNotAcceptableException when controller invokes the specific url. This works perfectly if i give the .json extention instead of .html.
I am using the following jars for json mapping
jackson-annotations-2.8.7.jar
jackson-core-2.8.7.jar
jackson-core-asl-1.9.13.jar
jackson-databind-2.8.7.jar
jackson-datatype-joda-2.8.7.jar
jackson-jaxrs-json-provider-2.8.7.jar
jackson-mapper-asl-1.9.13.jar
jackson-module-jaxb-annotations-2.8.7.jar
json-simple-1.1.jar
Following is my java based config instead of web.xml
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "*.html", "*.json"};
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setMultipartConfig(getMultipartConfigElement());
}
private MultipartConfigElement getMultipartConfigElement() {
MultipartConfigElement multipartConfigElement = new MultipartConfigElement( LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);
return multipartConfigElement;
}
private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.
// Beyond that size spring will throw exception.
private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.
private static final int FILE_SIZE_THRESHOLD = 0; // Size threshold after which files will be written to disk
}
My controller method is as below.
@RequestMapping(method = RequestMethod.POST, value = "/getDetails.html", produces = "application/json")
@ResponseBody
public String getDetails(@RequestBody String id, HttpServletRequest request,
HttpServletResponse response, Model model) {
logger.info("getDetails");
ObjectMapper mapper = new ObjectMapper();
// do something
}
}
I have an ajax call from where the url is invoked
var getDetails = function() {
var id = {
"id" : $("#id").val()
}
$
.ajax({
type : "POST",
url : "../data/getDetails.html",
data : JSON.stringify(id),
contentType : "application/json; charset=utf-8",
mimeType: "application/json",
dataType : 'json',
success : function(data) {
// do something
}
Following is from server log
INFO stdout:71 - 2017-10-16 12:13:53 DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher' processing POST request for [/App/data/getDetails.html]
DEBUG DispatcherServlet:861 - DispatcherServlet with name 'dispatcher' processing POST request for [/App/data/getDetails.html]
INFO stdout:71 - 2017-10-16 12:13:53 DEBUG RequestMappingHandlerMapping:320 - Looking up handler method for path /data/getDetails.html
DEBUG RequestMappingHandlerMapping:320 - Looking up handler method for path /data/getDetails.html
INFO stdout:71 - 2017-10-16 12:13:53 DEBUG ExceptionHandlerExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG ExceptionHandlerExceptionResolver:131 - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
INFO stdout:71 - 2017-10-16 12:13:53 DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'globalExceptionController'
DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'globalExceptionController'
Please help!
Upvotes: 0
Views: 780
Reputation: 8378
Remove produces
tag from @RequestMapping
annotation. then It will work.
Upvotes: 1
Reputation: 1682
Assuming that you are using default spring mvc config. It uses InternalResourceViewResolver by default. What you need to do is configure ContentNegotiatingViewResolver which tries to resolve views based on content type. I am not posting an example because you will find many on internet.
I would recommend that you read about REST endpoints and use proper notations and standards while writing your endpoints. For example, in your question you have an endpoint with .html which returns json response is quite correct.
Upvotes: 1
Reputation: 89
(I don't have access to comment on your post..so comment on my post..)
did you try consumes= "application/json"
like produces = "application/json"
in your annotations?
And
In your ajax call, you used both contentType and dataType. You are expecting response in json format right?
Upvotes: 1