【发布时间】:2016-05-24 08:40:12
【问题描述】:
我正在尝试创建一个资源(在使用 Jersey 的 REST Java Web 服务中)将ArrayList<String> 发送回用户。
我已经尝试了几个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>吗?
编辑:这是我的 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>
编辑:当我调用我的 getAll() 方法时,这些是我得到的以下日志:
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}
【问题讨论】:
-
我通常希望在这里看到
List<String>而不是ArrayList<String>。 -
感谢您的建议,我已将代码更改为
@GET @Produces(MediaType.APPLICATION_JSON) public List<String> getAll(){ List<String> result = new ArrayList<String>(); result.add("one"); result.add("two"); result.add("three"); return result; },但即使它是更好的代码,也没有解决我的问题 -
您是否收到任何错误,(与 messagebodywriter 相关),您也可以发送一个简单的字符串作为 application/json 吗? , 也许你可以发布你的 web.xml
标签: java web-services rest jersey jax-rs