View Full Version : HttpMediaTypeNotAcceptableException (always)
bcrescimanno
Feb 19th, 2010, 03:17 PM
Hi,
I'm new to SpringMVC and I'm working with version 3.0.1 as of today. I'm currently trying to use the new ajax functionality accessible via @RemoteBody.
I followed the posting on mvc-ajax (http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/?__utma=1.1116823469.1266441969.1266441969.1266589 454.2&__utmb=1.2.10.1266591727&__utmc=1&__utmx=-&__utmz=1.1266589454.2.2.utmcsr=google|utmccn=(orga nic)|utmcmd=organic|utmctr=springmvc%20ajax%20exam ple&__utmv=-&__utmk=40415266) to get up and running and attempted to output a nested list of objects. I was receiving HttpMediaTypeNotAcceptableExceptions so I decided to simplify as much as possible.
My controller method:
@RequestMapping(value="/accounthistory", method=RequestMethod.GET)
public @ResponseBody HistoryLineItem getHistoryViaJson() {
HistoryLineItem hItem = new HistoryLineItem("this is string a", "this is string b");
return hItem;
}
The HistoryLineItem class is just a simple value object--nothing but the most basic constructor, 2 string properties, and getters & setters. Even now, when I make the request, I'm receiving the same exception. I get this exception whether I make the request directly through the browser or using jQuery's $.ajax() method.
The only change other than adding the above handler method was to import the @ResponseBody annotation.
bcrescimanno
Feb 22nd, 2010, 08:18 AM
bump (and the only time I'll bump it).
I'm still having this issue; I really thought it might be related to the complexity of my object but the fact that it's happening even with just a simple object leads me to think something is more wrong.
bcrescimanno
Feb 22nd, 2010, 02:07 PM
Here is the associated stack trace:
[Mon Feb 22 15:03:08 2010] [ERROR] [http-8080-Processor22] com.company.billing.exceptions.BillingExceptionRes olver - Could not find acceptable representation
org.springframework.web.HttpMediaTypeNotAcceptable Exception: Could not find acceptable representation
at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter$ServletHandlerMethodIn voker.handleResponseBody(AnnotationMethodHandlerAd apter.java:886)
at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter$ServletHandlerMethodIn voker.getModelAndView(AnnotationMethodHandlerAdapt er.java:816)
at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.invokeHandlerMethod(An notationMethodHandlerAdapter.java:416)
at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:552)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
at com.brightcove.servlet.BrightcoveServletFilter.doF ilter(BrightcoveServletFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:637)
mohanlalit
Mar 6th, 2010, 04:09 PM
I am also facing the same problem have you found any solution to this exception.
Regards,
Odyssee
Mar 6th, 2010, 04:12 PM
Hi,
First, there is this bug on 3.0 RC1 version
http://issues.springframework.org/browse/SPR-6214?page=com.atlassian.jirafisheyeplugin%3Afishey e-issuepanel
And it seems to be resolved on 3.0 RC2...
But... I got the same error as you with the 3.0 RC2 version...
It's working when I return a simple string but not for an empty ArrayList<String>
Check if you have Jackson library in your classpath and <mvc:annotation-driven /> in you context file.
Otherwise, you can use directly the Jackson library:
String json = mapper.writeValueAsString(yourcomplexobject);
and return the json string as your requestbody...
mtam
Apr 21st, 2010, 04:33 PM
I am having the same problem.
I am using JAXB2 instead of JSON.
I have <mvc:annotation-driven /> in my context file.
The problem happened when a Response Object was returned from my controller which is using @ResponseBody.
@RequestMapping(value = "/httpreqmsg", method=RequestMethod.POST)
public @ResponseBody HttpRspMsg createResponse(@Valid HttpReqMsg httpReqMsg) {
// return a HttpRspMsg object
return httpReqMsg.createResponse(httpReqMsg);
}
Any solution found to this problem? Any info or ideas to help me out?
Many Thanks in advance.
TPub
May 19th, 2010, 06:46 PM
Did you resolve the issue? I'm facing the same problem.
cgof
May 29th, 2010, 11:20 AM
I am also having this problem. My controller receives JSON ajax request, but I get the media exception when trying to sen ANYTHING back (strings, ints, simple beans....).
It does however work if i explicitly writes the request as suggested above. But I wonder why it does not work for me in my application such as in the mvc-ajax example. (Im using Spring 3.0.2)
erl
Jun 17th, 2010, 04:12 AM
We had a similar issue. The root cause of our problem was that the object returned from the controller did not have method names matching the field names.
Verify that your return objects are proper javabeans where the methods are called getFoo() and getBar() for fields foo and bar respectively.
dare
Jun 23rd, 2010, 04:18 PM
In your client, make sure you are setting the HTTP Header for "Accept"
Accept: application/xml
(or json, or whatever is your content type).
Christian.Fischer
Jul 17th, 2010, 11:16 PM
Not mentioned here, but did it for me!
Working with a roo-generated Project in Progress.
Would be nice if somebody tries this with a fresh roo-project!
Solution
Your webflow-config.xml should contain
<!--Dispatches requests mapped to POJO @Controllers implementations-->
<bean class="org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJac ksonHttpMessageConverter"/>
</list>
</property>
</bean>
and your pom.xml needs to be extended
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.3.0</version>
</dependency>
please perform clean in roo and/or mvn clean or else!
My Testing
I am using the jetty with
mvn jetty:run
After MyController is enabled with
@RequestMapping(method = RequestMethod.POST, value = "/mycontroller/somextramapping") and
@ResponseBody
Queries could be sent via:
curl -v -X POST -H"Accept: application/json" -F "id=1" http://localhost:8080/myapp/mycontroller/somextramapping
a wrong OR NONE Accept-header will result again in error!
chen39yi
Jul 28th, 2010, 11:04 PM
I wish I could see the thread earlier. I got the hint from the Spring blog
http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
After I added the jackson dependency into my project
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.5.4</version>
</dependency>
Spring MVC returned the right json data to me.
And I do not think the any configuration is needed if you have the <mvc:annotation-driven/>, which does almost everything for you.
joel.rosi-schwatz
Oct 5th, 2010, 12:24 PM
Hi,
I ran into this issue today and it drove me to distraction for a bit :confused: Till I finally figured it out. I'll share my experience in case it saves someone else the pain.
The key is that this usage was in an EAR loaded into Weblogic 11g. My guess is that one will get similar behaviour regardless of the Java EE container, but I am not certain of that.
My Controller is, of course, located in the web module and packaged in the war. When I added the Jackson dependency to the web module it was never resolved. Unfortunately the Spring rest implementation simply complains with a HttpMediaTypeNotAcceptableException with no indication that jackson-mapper is not found. I finally figured out the issue when I explicitly wired a JasonView which gave a clear ClassNotFoundException on ObjectMapper.
Pretty odd I thought seeings that the jars were in WEB-INF/lib but it got me wondering. I removed the dependency from the the web module and added it to the ear module. Everything started working as it should.
I would very much like to know why it has to be in the ear rather than the war. Any one willing to enlighten me?
hth,
Joel
rochsat
Dec 16th, 2010, 01:48 PM
Hi, I am new to spring framework and I am facing the same problem of HttpMediaTypeNotAcceptableException. I am using castor generated resources. My castor marshell/unmarshell configuration (applicationContext.xml) are as below.
<bean
class="org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringHttpMessageConverter"/>
<ref bean="marshallingHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessa geConverter" />
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.Marshalling HttpMessageConverter">
<property name="marshaller" ref="castorMarshaller" />
<property name="unmarshaller" ref="castorMarshaller" />
</bean>
<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />
All the castor jars and resources are in WEB-INF/lib folder. I tried sending a string and it works out well. Not sure where I am doing wrong. My java code looks as follows.
@RequestMapping(value="/", method=RequestMethod.GET)
public @ResponseBody ServiceRoot getServiceRoot() {
System.out.println("received request.");
return rootService_.getServiceRoot();
}
ServiceRoot is the Castor generated class. Any kind of help appreciated!... Also, an example code would work just fine as well!
javib00
Jan 16th, 2011, 03:42 PM
Hi, I am also having this problem. My controller receives JSON ajax request, but I get the media exception when trying to send back simple beans.
The cause of my problem was that the object returned from the controller did not have method names matching for boolean fields. I have a method isEnabled() instead of getEnabled().
Torquester
Jul 6th, 2011, 04:15 AM
If you're using roo and STS it is sufficient to execute following commands in the roo shell
dependency add --groupId org.codehaus.jackson --artifactId jackson-jaxrs --version 1.5.4
perform eclipse
I did not have to update any other config files.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.