1

I've been working on Spring 3.0 web application using Netbeans and Glassfish server. I'm now to the point where I should be deploying the application for public use, however in attempting to move from Glassfish to Tomcat I'm running into an error. My application uses AJAX to grab information from a URL within the application but when I request the url I get the following:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

My controller for the information it is requesting looks like so:

@RequestMapping(value = "/electricity/usage/")
public @ResponseBody List<UsageData> getEUsage(HttpSession session) {

    UsageDataDAO UsageDAO = new UsageDataDAO();

    User u = (User) session.getAttribute("user");

    List<UsageData> l = UsageDAO.getAllUsageData(u.getAccountNum(), 'e');

     return l;
}

Why is this happening when I migrate to Tomcat?

Chris Maness
  • 1,682
  • 3
  • 22
  • 40

1 Answers1

0

This error happens because what your browser sends to the server in the "Accept" header does not match what the server can return from the URL /electricity/usage/

If you are using a library like jQuery (and NOT doing cross-domain requests, this is important) then your browser will send this accept header:

Accept: application/json

You getting that error means that the server does not think the URL /electicity/usage can return a JSON response. This is configured in your webmvc-config.xml file. This is what I have in mine related to JSON requests:

<!-- allows rendering responses in XML and JSON formats -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="application/xml"/>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller">
                    <bean class="org.springframework.oxm.xstream.XStreamMarshaller" p:autodetectAnnotations="true"/>
                </property>
            </bean>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
        </list>
    </property>
</bean>

See this tutorial for implementing JSON responses in Spring MVC: http://rwehner.wordpress.com/2010/06/09/2-ways-to-create-json-response-for-ajax-request-in-spring3/

Why this code works on Glassfish, but not Tomcat - that is a good question...

If you are doing a cross-domain request, then this topic applies: JQuery's getJSON() not setting Accept header correctly?

Community
  • 1
  • 1
anton1980
  • 979
  • 3
  • 10
  • 20
  • That's just the thing. There's no evidence that I have anything set up incorrectly. I'm using the @responsebody handler just like in the link. The code works flawlessly on glassfish but throws an error in tomcat. – Chris Maness Mar 23 '12 at 17:46
  • Would this article help? http://www.gotoquiz.com/web-coding/programming/java-programming/jsonajax-in-spring-mvc/ – anton1980 Mar 23 '12 at 21:24
  • Also, I would use Firebug in FF to make sure that your browser has Accept: application/json header when making this AJAX request. If it does, then you can be sure the problem is application configuration, which does not know that your URL /electricity/usage/ is supposed to return application/json content type. – anton1980 Mar 23 '12 at 21:25