2011-01-29 8 views
6

せずに私はRESTfulなWebサービスを持っており、応答は次のとおりです。JAX-RS - JSONルート・ノード

{ 
    "cities": [{ 
     "id": "1", 
     "name": "City 01", 
     "state": "A1" 
    }, { 
     "id": "2", 
     "name": "City 02", 
     "state": "A1" 
    }] 
} 

しかし、私はこれ欲しい:私はJAX-RSを設定するにはどうすればよい

{ 
    [{ 
     "id": "1", 
     "name": "City 01", 
     "state": "A1" 
    }, { 
     "id": "2", 
     "name": "City 02", 
     "state": "A1" 
    }] 
} 

を実装固有の機能ではなく、JAX-RS機能のみを使用してルートノードなしでJSONを生成しますか?私のコードは、どのアプリケーションサーバーでも移植可能でなければなりません。

+0

どのようにあなたのモデル(市)クラスJAXB注釈付きますか?これは、XMLとJSON間のシリアル/デシリアライゼーションを制御するためのものです。 –

+0

@ XmlRootElement(name = "cities") public class CityDTO はSerializable { } –

答えて

4

Glassfish v3でも同じ問題がありました。私はこの動作がJAX-RS実装に依存し、CodehausのジャクソンJAX-RS実装への切り替えが私の問題を解決したことを発見しました。

<!-- REST --> 

<servlet> 
    <servlet-name>RESTful Services</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
    <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name> 
    <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value> 
    </init-param> 
    <init-param> 
    <param-name>com.sun.jersey.config.property.packages</param-name> 
    <param-value>you.service.packages;org.codehaus.jackson.jaxrs</param-value> 
    <!-- NOTE: The last element above, org.codehaus.jackson.jaxrs, replaces the default 
     JAX-RS processor with the Codehaus Jackson JAX-RS implementation. The default 
     JAX-RS processor returns top-level arrays encapsulated as child elements of a 
     single JSON object, whereas the Jackson JAX-RS implementation return an array. 
    --> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>RESTful Services</servlet-name> 
    <url-pattern>/your/rest/path/*</url-pattern> 
</servlet-mapping> 

を別の方法として、あなたは、単にすることができるかもしれない:あなたにもGlassfishのを使用している場合

、あなたは次のように戦争にorg.codehaus.jackson.jaxrsを追加することによってだけでなく、WEB-INF/web.xml構成に問題を解決することができますクライアントでの応答をインターセプト:

function consumesCity(json) { 
    ... 
} 

が置き換え

... consumesCity(json) ... 

function preprocess(json) { 
    return json.city; 
} 

... consumesCity(preprocess(json)) ... 
+1

を実装しています。しかし、私はまだポータブルなソリューションを探しています。 –

+3

私は、この特定の動作がベンダー固有であるという印象を受けました。つまり、コレクションの戻り値の型のトップレベルJSONオブジェクトが配列か、コレクションメンバーを含むフィールドを持つ単一のオブジェクトかどうかです。私はGlassfishのリファレンス実装がコレクションの後者のスタイルを返すのを見て少し驚いた。私はJSON配列を返すほうがはるかに直感的だと思います。より良い解決策を見つけたら、アップデートを投稿してください。 –

+1

私は金に同意します。主に歴史的な理由から、構造体は実際には異なりますが、具体的にはXML APIを使用してJSONを処理するlibsがあります。これはラッパーの使用を強制する場合があります。カスタムを使用しない限り、実際には標準的な方法はありませんMessageBodyWriter/Readerの実装。 – StaxMan

1

良い質問。私はこれに似た要件を持っていました。私は生成された生の応答にアクセスし、何らかの操作をしなければならなかった。私は、レゾナンスフィルタを登録し、カスタムレポマシンを登録することでそれを達成しました。詳細は以下のリンクを参照してください。あなたの応答フィルタで

http://www.mentby.com/paul-sandoz/access-to-raw-xml-in-jersey.html 

、あなたが生成されたJSONからクラス名を切り抜く、またはより良いまだ、応答文字列を返すとGoogle-gsonのようなカスタムJSONのシリアル化メカニズムを使用することができます。

このソリューションが動作するかどうかを教えてください。

0

上記のKim Burgaardの回答は、Jersey Spring WSでも同様です。私はGlassfish 3.0を使用して同じ問題を抱えており、以下に示すパラメータを追加して解決しました。

例のweb.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>Jersey Spring Web Application</servlet-name> 
     <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>org.codehaus.jackson.jaxrs</param-value> 
<!--    NOTE: The last element above, org.codehaus.jackson.jaxrs, replaces the default 
       JAX-RS processor with the Codehaus Jackson JAX-RS implementation. The default 
       JAX-RS processor returns top-level arrays encapsulated as child elements of a 
       single JSON object, whereas the Jackson JAX-RS implementation return an array.--> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey Spring Web Application</servlet-name> 
     <url-pattern>/services/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

例applicationContext.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:p="http://www.springframework.org/schema/p" 
     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation=" 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
    <!-- Scan for both Jersey Rest Annotations and persistence classes --> 
    <context:component-scan base-package="your.service.packages"/> 
</beans> 
関連する問題