2010-12-07 14 views
10

私はちょうどVirgo Web Serverの学習を開始しました。 Spring MVCアプリケーションでJakcson JSONを使用しようとしています。 この段階では、GETリクエストのシリアル化されたオブジェクトを取得できません。 サーバーが返す「406容認できない」:406 JAVA、ローマ、JAXB2を使用してSpring MVCアプリケーション(OSGi、Virgo Web Server)で使用できない

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

ローマと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> 

ディスパッチャ〜 t.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 

答えて

2

詳細はhttp://www.checkupdown.com/status/E406.htmlを参照してください。クライアントアプリケーションは、返送されるデータの種類を受け入れないことをサーバーに伝えています。

私はあなたが使っているlibsなどに慣れていませんが、あなたが受け入れるヘッダをプログラム的に(またはFirebugのようなものを通して)見ることができます。うまくいけば、ソースコード/設定でそれを見つけることができます。

あなたのクライアントがJSONの復帰を要求しており、サーバーが送信していないと思われます。

+0

@EnableWebMvc 

@Configurationクラスに注釈を付けるために逃しました。ご回答いただきありがとうございます。 – Alexey

+0

@Alexey - OK、これは興味深いことですが、あなたが正しい受け入れヘッダを送信しているように見えます。理論的には、サーバーがjsonまたはjavascriptを返さないということです。私は個人的にまだSpring Webサービスのものを使用していませんが、本当にjsonであることを確認するために応答を傍受/検査する方法はありますか? –

16

のような新しい注釈を使用するためには、には<mvc:annotation-driven>が含まれていることを確認してください。

また、文脈設定にいくつかの混乱があります。dispatcher-servlet.xmlDispatcherServletのコンテキストを設定するために使用されていますが、親コンテキストのcontextConfigLocationには指定しないでください。

+0

編集したdispatcher-servlet.xmlに間違ったファイルを投稿しました。はい、dispatcher-servlet.xmlでを使用します。親コンテキストのcontextConfigLocationをapplicationContext.xmlに変更しました。これはDispatcherServletのコンテキストでのみ使用されるdispatcher-servlet.xmlです。しかし、エラーは続く。ご回答いただきありがとうございます。 – Alexey

+2

@Alexey:明示的に 'AnnotationMethodHandlerAdapter'を宣言していないことを確認してください。 – axtavt

+0

@axtavtあなたのソリューションは完璧に動作しました、ありがとう! :) – ManiSto

0

@axtavt AnnotationMethodHandlerAdapterの明示的な宣言をしていないということを教えてください。サーブレットファイルで次のような宣言をしていますが、私は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> 
23

スプリングは、jsonコンバータが見つからない場合には406を返します。

はジャクソンの瓶が実際にWebアプリケーションのlibディレクトリに展開されていることを確認します。私の場合はこれが問題でした。

1

私は同じ問題に遭遇して、次のことが原因である可能性があります場合、私は疑問に思って。

AnnotationDrivenBeanDefinitionParserクラスはJAXB2 /ジャクソンの可用性のためのクラスパスをチェックする担当しています。

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

通常、バンドルのアプリケーションコンテキストは、osgi対応のクラスローダーを提供します。しかし、jaxb2present変数は静的であるため、アプリケーション・コンテキストによってインスタンス化される前に、クラスがロードされたときに静的に設定されています。その時点でクラスローダーはosgi対応ではないので、探しているクラスを見つけることができません。今の

、私は唯一の回避策は、手動で、私はこれは406エラーを返し

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

public @ResponseBody 

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

} 

:-)まだやる方法がわからないどのJaxb2HttpMessageConverterを(アップ配線すると仮定しています。戻り値の型をintにすると、問題は消えてしまいました。

3

私は正確に同じ問題を抱えていたと私は必要なすべては私のJavaクラスのパブリックゲッターを入れていた

参照:https://stackoverflow.com/a/18228476/20654

+1

これは私の場合でした、ありがとう! –

+0

私も同様の問題を抱えていて、それも私の場合でした –

1
<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;バージョン= "[3.0.0、4.0.0)"、org.codehaus.jackson.map;バージョン= "[1.4.3,1.4.3]"

0

あなただけ必要@ResponseBodyを取り除く

0

戻りたいJavaオブジェクトにクラス定義の上にXmlRootElementが付いていないため、406エラーが発生したのと同じ問題が発生しました。 Iが要求に受け入れ=アプリケーション/ JSONヘッダを含まれるが、私はヘッダーを変更し、表示するPostman使用要求

に受け入れ=アプリケーション/ XMLヘッダを含ま場合406を返した場合 方法は問題なくJSONを返さ反応。非常に便利なツール。

0

私は同じエラーが発生しました。しかし、私はJavaアノテーションを使用しました。私はすべての必要な依存関係を持っています。しかし、私はFirebugのからのリクエストヘッダのデータを追加しました

関連する問題