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()メソッドを呼び出すと、これらは私が得る次のログです
通常、 'ArrayList'ではなく 'List 'が表示されます。 –
khelwood
おかげでアドバイスを、私は 公共リストの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返品の結果; } 'しかし、それが良いコードであっても、私の問題は解決しませんでした –
(messagebodywriterに関連して)何かエラーがありますか?application/jsonとして単純な文字列を送ることができますか? 、おそらくあなたのweb.xmlを投稿することができます – gaurav5430