Java 406在使用Jackson、Rome和JAXB2的Spring MVC应用程序(OSGi、Virgo Web服务器)中不可接受

Java 406在使用Jackson、Rome和JAXB2的Spring MVC应用程序(OSGi、Virgo Web服务器)中不可接受,java,spring-mvc,jackson,jaxb2,rome,Java,Spring Mvc,Jackson,Jaxb2,Rome,我刚开始学习Virgo网络服务器。 我试图在SpringMVC应用程序中使用JakCSonJSON。 在此阶段,我无法获取get请求序列化对象。 服务器返回“406不可接受”: 使用Rome和JAXB2时也会出现同样的问题 以下是项目配置文件和代码: 片段pom.xml: <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>com.springsource.org

我刚开始学习Virgo网络服务器。 我试图在SpringMVC应用程序中使用JakCSonJSON。 在此阶段,我无法获取get请求序列化对象。 服务器返回“406不可接受”:

使用Rome和JAXB2时也会出现同样的问题

以下是项目配置文件和代码:

片段pom.xml:

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>com.springsource.org.codehaus.jackson</artifactId>
  <version>1.0.0</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>com.springsource.org.codehaus.jackson.mapper</artifactId>
  <version>1.0.0</version>
</dependency>
web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

  <welcome-file-list>
    <welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
  </welcome-file-list>

  <!-- CONFIGURE A PARENT APPLICATION CONTEXT -->
  <context-param>
    <param-name>contextClass</param-name>
    <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- DISPATCHER SERVLET CONFIG -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

</web-app>
index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
   <title>Simple jsp page</title>
   <script type="text/javascript" src="/greenpages/scripts/jquery-1.4.4.min.js"></script>
   <script type="text/javascript">
  $.getJSON("json.htm", function(message) {
   console.log(message);
  });
   </script>
  </head>
  <body>

  <form action="test.htm" method="get">
      <input type="text" name="name">
      <input type="submit">
  </form>

  </body>
</html>
响应标题:

HTTP/1.1 406 Not Acceptable
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1070
Date: Tue, 07 Dec 2010 11:15:58 GMT
可能出现什么问题?

有关详细信息,请参阅。您的客户端应用程序正在告诉服务器,它将不接受发送回的数据类型

我不熟悉您正在使用的libs等,但您应该能够通过编程(或通过类似Firebug的方式)查看您的accept头,以查看正在设置什么。希望您能在源代码/配置中找到这一点

我猜想您的客户端要求返回JSON,而您的服务器没有发送它。

确保
dispatcher servlet.xml中有
-它将Spring配置为使用新注释,例如
@ResponseBody


另外,我发现您在上下文配置方面有些混乱-
DispatcherServlet.xml
用于配置
DispatcherServlet
的上下文,不应在父上下文的
contextConfigLocation
中指定它。

@axtavt您能告诉我在AnnotationMethodHandlerAdapter上没有显式声明是什么意思吗。我的servlet文件中有以下声明,得到404

<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

<bean id="jsonConverter"
    class="com.clickable.pro.data.bll.internal.response.MyMappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="jaxbJacksonObjectMapper" />
    <property name="prefixJson" value="true"></property>
    <property name="prefixJsonString" value="while(1);"></property>
</bean>

如果找不到json转换器,Spring会返回406


验证jackson JAR是否已实际部署到webapp的lib目录。这就是我的问题所在。

我遇到了同样的问题,我想知道是否有以下原因

AnnotationDrivenBandeFinitionParser类负责检查类路径的jaxb2/jackson可用性。它使用以下逻辑执行此操作:

private static final boolean jaxb2Present =
        ClassUtils.isPresent("javax.xml.bind.Binder", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
现在,包的应用程序上下文通常会提供一个支持osgi的类加载器。但是,jaxb2present变量是静态的,因此在应用程序上下文实例化之前,在加载类时静态地设置它。我怀疑在那个时候,类加载器不知道osgi,所以它找不到它正在寻找的类

目前,我假设唯一的解决方法是手动连接一个Jaxb2HttpMessageConverter(我还不知道怎么做:-)


这将返回406错误。当我将return typeint转换为String时,问题就消失了。

我遇到了完全相同的问题,我所需要的就是在java类中放置公共getter

见:

.springsource.org.codehaus.jackson.mapper;version=“[1.4.3,1.4.3]”


go.web.dm;version=“[3.0.0,4.0.0]”,org.codehaus.jackson.map;version=“[1.4.3,1.4.3]”

您只需要去掉@ResponseBody

我遇到了一个类似的问题,我遇到了一个406错误,因为我试图返回的java对象没有在类定义上方用XmlRootElement注释。 当我将Accept=application/JSON头包含到请求中时,该方法返回了JSON而没有问题,但当我将Accept=application/xml头包含到请求中时,该方法返回了406


我曾经更改标题并查看响应。这是一个非常方便的工具。

我也有同样的错误。但是我使用Java注释,我有所有必要的依赖项。但是我没有使用注释

@EnableWebMvc 

我的
@Configuration
class

在dispatcher-servlet.xml中发布了错误的文件,已编辑。是的,我在dispatcher-servlet.xml中使用。我将父上下文的contextConfigLocation更改为applicationContext.xml,现在dispatcher-servlet.xml仅用于DispatcherServlet的上下文。但是错误仍然存在。谢谢您的回复。@Al埃西:确保您没有明确声明
AnnotationMethodHandlerAdapter
。之前有过这个问题。我们有过,但出于某种原因,您似乎需要mvc。我添加了Firebug的请求头数据。谢谢您的回复。@Alexey-好的,现在很有趣,看起来您确实在发送装备ht accept Header。这意味着理论上你的服务器没有返回json或javascript。我个人还没有使用Spring web services的东西,但是有没有一种方法可以拦截/检查响应以确保它确实是json?我也有类似的问题,我也是这样
GET /greenpages/json.htm HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://localhost:8080/greenpages/
Cookie: JSESSIONID=18000E4E096D7978F61F5D1E8105B784; JSESSIONID=35FB0925786699EC587A1B64F30517AD
HTTP/1.1 406 Not Acceptable
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 1070
Date: Tue, 07 Dec 2010 11:15:58 GMT
<bean
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jsonConverter" />
        </list>
    </property>
</bean>

<bean id="jsonConverter"
    class="com.clickable.pro.data.bll.internal.response.MyMappingJacksonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json" />
    <property name="objectMapper" ref="jaxbJacksonObjectMapper" />
    <property name="prefixJson" value="true"></property>
    <property name="prefixJsonString" value="while(1);"></property>
</bean>
private static final boolean jaxb2Present =
        ClassUtils.isPresent("javax.xml.bind.Binder", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
@RequestMapping(value = "/{cid}/{lid}/newslice", method = RequestMethod.POST)

public @ResponseBody

int addSlice(@PathVariable int cid, @PathVariable int lid, @RequestParam("t") String t) {

}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">


<context:component-scan base-package="com.roshka.osgi.controller">

</context:component-scan>



<mvc:annotation-driven />
<context:annotation-config />
Import-Bundle: org.eclipse.virgo.web.dm,com
Import-Package: javax.servlet;version="[3.0.0, 3.5.0)",org.eclipse.vir
@EnableWebMvc