2016-05-24 3 views
0

ArrayList<String>をユーザーに返すリソース(Jerseyを使用したREST Java Webサービス内)を作成しようとしています。Return arrayList <String>メディアタイプJAX-RS

私はいくつか試しましたMediaTypeしかし、私はArrayList<String>を簡単に返信できるようにするために使用するはずのものを見つけることができません。ここで

は私の問題を説明するコードサンプルです:

@Path("duck") 
public class DuckResource extends ResourceMongoDB{ 

@GET 
@Produces(MediaType.APPLICATION_JSON) 
public ArrayList<String> getAll(){ 
    ArrayList<String> result = new ArrayList<String>(); 
    result.add("one"); 
    result.add("two"); 
    result.add("three"); 
    return result; 
} 
} 

あなたは簡単にArrayList<String>を返送する方法任意のアイデアを持っていますか?

EDIT:ここに私のpom.xmlファイルがある

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 

<groupId>com.example.rest</groupId> 
<artifactId>jersey-service</artifactId> 
<packaging>jar</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>jersey-service</name> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.glassfish.jersey</groupId> 
      <artifactId>jersey-bom</artifactId> 
      <version>${jersey.version}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 
<dependency> 
    <groupId>postgresql</groupId> 
    <artifactId>postgresql</artifactId> 
    <version>8.3-606.jdbc3</version> 
</dependency> 
<dependency> 
    <groupId>org.mongodb</groupId> 
    <artifactId>mongo-java-driver</artifactId> 
    <version>3.2.2</version> 
</dependency> 
<dependency> 
      <groupId>org.mongodb.morphia</groupId> 
      <artifactId>morphia</artifactId> 
      <version>1.1.0</version> 
    </dependency> 
<dependency> 
    <groupId>com.google.code.gson</groupId> 
    <artifactId>gson</artifactId> 
    <version>2.6.2</version> 
</dependency> 
<dependency> 
    <groupId>org.glassfish.jersey.media</groupId> 
    <artifactId>jersey-media-moxy</artifactId> 
</dependency> 
    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-grizzly2-http</artifactId> 
    </dependency>   
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.9</version> 
     <scope>test</scope> 
    </dependency> 
<dependency> 
    <groupId>commons-lang</groupId> 
    <artifactId>commons-lang</artifactId> 
    <version>2.6</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-server</artifactId> 
    <version>1.8</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-json</artifactId> 
    <version>1.8</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-core</artifactId> 
    <version>1.8</version> 
</dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.5.1</version> 
      <inherited>true</inherited> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <version>1.2.1</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>java</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <mainClass>main.Main</mainClass> 
      </configuration> 
     </plugin> 
    </plugins> 
<resources> 
    <resource> 
     <directory>src/main/java/resources</directory> 
    </resource> 
</resources> 
</build> 

<properties> 
    <jersey.version>2.17</jersey.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

EDIT:

juin 08, 2016 10:09:58 AM com.mongodb.diagnostics.logging.JULLogger log 
INFOS: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500} 
juin 08, 2016 10:09:58 AM com.mongodb.diagnostics.logging.JULLogger log 
INFOS: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=50} 
juin 08, 2016 10:09:58 AM com.mongodb.diagnostics.logging.JULLogger log 
INFOS: Opened connection [connectionId{localValue:1, serverValue:51}] to 127.0.0.1:27017 
juin 08, 2016 10:09:58 AM com.mongodb.diagnostics.logging.JULLogger log 
INFOS: Opened connection [connectionId{localValue:2, serverValue:52}] to localhost:27017 
juin 08, 2016 10:09:58 AM com.mongodb.diagnostics.logging.JULLogger log 
INFOS: Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 6]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=424584} 
juin 08, 2016 10:09:58 AM com.mongodb.diagnostics.logging.JULLogger log 
INFOS: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 6]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=389829} 
:私は私のgetAll()メソッドを呼び出すと、これらは私が得る次のログです
+1

通常、 'ArrayList 'ではなく 'List 'が表示されます。 – khelwood

+0

おかげでアドバイスを、私は 公共リストのgetAll(){ \t \t一覧結果=新しいArrayListを()\t '@GET @Produces(MediaType.APPLICATION_JSON)に自分のコードを変更しました。 \t \t result.add( "one"); \t \t result.add( "two"); \t \t result.add( "three"); \t \t返品の結果; } 'しかし、それが良いコードであっても、私の問題は解決しませんでした –

+0

(messagebodywriterに関連して)何かエラーがありますか?application/jsonとして単純な文字列を送ることができますか? 、おそらくあなたのweb.xmlを投稿することができます – gaurav5430

答えて

1

私はgensonを使用して解決策を見つけました。

私はPOMファイルに次の依存関係を追加しました:私は私の問題を共有する必要がある場合があります願っています

Response.status(Status.OK).entity(result).build(); 

出来上がり、

<dependency> 
    <groupId>com.owlike</groupId> 
    <artifactId>genson</artifactId> 
    <version>1.4</version> 
</dependency> 

と対応の内側に私のresultをカプセル化他人を助けた!

-1

次のようにメソッドを変更したい場合があります。

@GET 
@Path("/getAll") 
@Produces(MediaType.APPLICATION_JSON) 
public Response getAll() { 
    List<String> result = new ArrayList<String>(); 
    result.add("one"); 
    result.add("two"); 
    result.add("three"); 
    GenericEntity<List<String>> genericEntity = 
     new GenericEntity<List<String>>(result) {}; 
    return Response.ok(genericEntity).build(); 
} 

私のpom.xmlでの依存関係は以下の通りです:

<dependencies> 
    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-server</artifactId> 
     <version>1.8</version> 
    </dependency> 
    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-json</artifactId> 
     <version>1.8</version> 
    </dependency> 
    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-core</artifactId> 
     <version>1.8</version> 
    </dependency> 
</dependencies> 
+0

私は私の答えにいくつかのわずかな修正を加えました。これをTomcatで実行している場合は、適切なライブラリが必要です(上記のpom.xmlに記載されているように)。そうでなければ、エラー500が発生します。URLはgetAll.jsonを出力します。 2つの "、" 3つの "]。 – BlackKnight

+0

私はこの作業を行うことができますので、なぜダウン票ですか? – BlackKnight

+0

こんにちは、申し訳ありませんが返信する長い時間です。私はあなたのアドバイスを試しましたが、悲しいことに私はそれが私のために働くように管理することはできません。私はpostgresql、mongodb、morphia、glassfishジャージーメディア、グラスフィッシュジャージーコンテナー、junit、コモンズラン、さらにあなたが私に与えた3つの依存関係を持っています。私は私の質問に私の完全なpom.xmlファイルを追加します:) –

関連する問題