将Spring应用程序从Glassfish移动到Tomcat

时间:2021-09-29 13:58:51

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:

我一直在使用Netbeans和Glassfish服务器处理Spring 3.0 Web应用程序。我现在要将应用程序部署为公共使用,但是在尝试从Glassfish迁移到Tomcat时,我遇到了错误。我的应用程序使用AJAX从应用程序中的URL获取信息,但是当我请求URL时,我得到以下内容:

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

此请求标识的资源只能根据请求“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?

迁移到Tomcat时为什么会发生这种情况?

1 个解决方案

#1


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/

发生此错误的原因是您的浏览器在“Accept”标头中发送到服务器的内容与服务器可以从URL /电/用途/返回的内容不匹配

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:

如果您使用的是像jQuery这样的库(并且不执行跨域请求,这很重要),那么您的浏览器将发送此接受标头:

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:

您收到该错误意味着服务器不认为URL / electicity / usage可以返回JSON响应。这是在webmvc-config.xml文件中配置的。这就是我在JSON请求中的相关内容:

<!-- 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/

请参阅本教程以在Spring MVC中实现JSON响应: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...

为什么这段代码适用于Glassfish,而不是Tomcat - 这是一个很好的问题......

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

如果您正在进行跨域请求,那么本主题适用:JQuery的getJSON()没有正确设置Accept头?

#1


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/

发生此错误的原因是您的浏览器在“Accept”标头中发送到服务器的内容与服务器可以从URL /电/用途/返回的内容不匹配

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:

如果您使用的是像jQuery这样的库(并且不执行跨域请求,这很重要),那么您的浏览器将发送此接受标头:

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:

您收到该错误意味着服务器不认为URL / electicity / usage可以返回JSON响应。这是在webmvc-config.xml文件中配置的。这就是我在JSON请求中的相关内容:

<!-- 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/

请参阅本教程以在Spring MVC中实现JSON响应: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...

为什么这段代码适用于Glassfish,而不是Tomcat - 这是一个很好的问题......

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

如果您正在进行跨域请求,那么本主题适用:JQuery的getJSON()没有正确设置Accept头?