簡體   English   中英

406使用Jackson,Rome和JAXB2在Spring MVC應用程序(OSGi,Virgo Web Server)中不可接受

[英]406 Not Acceptable in Spring MVC application (OSGi, Virgo Web Server) using Jackson, Rome and JAXB2

我剛開始學習Virgo Web Server。 我正在嘗試使用Spring MVC應用程序中的Jakcson JSON。 在這個階段,我無法獲得GET請求序列化對象。 服務器返回“406 Not Acceptable”:

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

使用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>

MANIFEST.MF

Manifest-Version: 1.0
Import-Bundle: com.springsource.org.apache.taglibs.standard;version="[
 1.1.2,1.3)",com.springsource.org.codehaus.jackson;version="[1.0.0,1.0
 .0]",com.springsource.org.codehaus.jackson.mapper;version="[1.0.0,1.0
 .0]"
Bundle-Version: 2.3.0
Tool: Bundlor 1.0.0.RELEASE
Bundle-Name: GreenPages Web
Import-Library: org.springframework.spring;version="[3.0, 3.1)"
Bundle-ManifestVersion: 2
Bundle-SymbolicName: greenpages.web
Web-ContextPath: greenpages
Import-Package: javax.servlet.jsp.jstl.core;version="[1.1.2,1.2.0)",ja
 vax.sql,org.apache.commons.dbcp,org.eclipse.virgo.web.dm;version="[2.
 0.0, 3.0.0)",org.springframework.core.io;version="[3.0.0.RELEASE,3.1.
 0)",org.springframework.stereotype;version="[3.0.0.RELEASE,3.1.0)",or
 g.springframework.ui;version="[3.0.0.RELEASE,3.1.0)",org.springframew
 ork.web.bind.annotation;version="[3.0.0.RELEASE,3.1.0)",org.springfra
 mework.web.servlet.mvc.annotation;version="[3.0.0.RELEASE,3.1.0)",org
 .springframework.web.servlet.view;version="[3.0.0.RELEASE,3.1.0)"

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>

調度員servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="greenpages.web"/>

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

GreenPagesController.java

package greenpages.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GreenPagesController {

 @RequestMapping("/home.htm")
 public void home() {
 }

 // MappingJacksonHttpMessageConverter (requires Jackson on the classpath - particularly useful for serving JavaScript clients that expect to work with JSON)
 @RequestMapping(value="/json.htm", method=RequestMethod.POST)
 public @ResponseBody String readJson(@RequestBody JavaBean bean) {
  return "Read from JSON " + bean;
 }

 @RequestMapping(value="/json.htm", method=RequestMethod.GET)
 public @ResponseBody Object writeJson() {
  return new Object();
 }

}

的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>

AJAX請求http:// localhost:8080 / greenpages / json.htm :來自Firebug的請求標頭:

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

可能是什么問題?

如果找不到json轉換器,Spring會回退到406。

驗證jackson jar實際上是否已部署到webapp的lib目錄中。 這是我的問題。

確保在dispatcher-servlet.xml<mvc:annotation-driven> - 它將Spring配置為使用新的注釋,例如@ResponseBody

此外,我發現您在上下文配置中有一些混淆 - dispatcher-servlet.xml用於配置DispatcherServlet的上下文,不應在父上下文的contextConfigLocation中指定。

我有完全相同的問題,我需要的是將公共getter放入我的java類

請參閱: https//stackoverflow.com/a/18228476/20654

有關詳細信息,請參見http://www.checkupdown.com/status/E406.html 您的客戶端應用程序告訴服務器它不會接受發回的數據類型。

我不熟悉您正在使用的庫等,但您應該能夠以編程方式(或通過Firebug之類的東西)查看您的接受標頭,以查看正在設置的內容。 您可以在源代碼/配置中找到它。

猜測我希望你的客戶要求JSON回來而你的服務器沒有發送它。

<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

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

Import-Package: javax.servlet;version="[3.0.0, 3.5.0)",org.eclipse.vir

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

我遇到了同樣的問題,我想知道以下是否可能是原因。

AnnotationDrivenBeanDefinitionParser類負責檢查jaxb2 / jackson可用性的類路徑。 它使用以下邏輯來執行此操作:

private static final boolean jaxb2Present =
        ClassUtils.isPresent("javax.xml.bind.Binder", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());

現在,通常bundle的應用程序上下文將提供一個osgi感知的類加載器。 但是,jaxb2present變量是靜態的,因此在加載類之前,它會在由應用程序上下文實例化之前進行靜態設置。 我懷疑在那個時候,類加載器不是osgi感知的,所以它找不到它正在尋找的類。

現在,我假設唯一的解決方法是手動連接Jaxb2HttpMessageConverter(我不知道該怎么做:-)

@RequestMapping(value = "/{cid}/{lid}/newslice", method = RequestMethod.POST)

public @ResponseBody

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

}

這將返回406錯誤。 當我將返回類型int轉換為String時,問題就消失了。

你只需要擺脫@ResponseBody

我有一個類似的問題,我得到406錯誤,因為我試圖返回的java對象沒有使用類定義上方的XmlRootElement注釋。 當我將Accepts = application / json標頭包含在請求中時,該方法返回JSON沒有問題,但當我將Accept = application / xml標頭包含在請求中時返回406

我使用Postman來改變標題並查看回復。 非常方便的工具。

我有同樣的錯誤。 但我使用Java Annotations,我有所有必要的依賴。 但我錯過了注釋

@EnableWebMvc 

我的@Configuration課程

@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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM