【问题标题】:Return arrayList<String> media type JAX-RS返回 arrayList<String> 媒体类型 JAX-RS
【发布时间】:2016-05-24 08:40:12
【问题描述】:

我正在尝试创建一个资源(在使用 Jersey 的 REST Java Web 服务中)将ArrayList&lt;String&gt; 发送回用户。

我已经尝试了几个MediaType,但我找不到我应该使用哪个以便能够轻松地发回我的ArrayList&lt;String&gt;

这是一个说明我的问题的代码示例:

@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&lt;String&gt;吗?

编辑:这是我的 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&lt;String&gt; 而不是ArrayList&lt;String&gt;
  • 感谢您的建议,我已将代码更改为 @GET @Produces(MediaType.APPLICATION_JSON) public List&lt;String&gt; getAll(){ List&lt;String&gt; result = new ArrayList&lt;String&gt;(); result.add("one"); result.add("two"); result.add("three"); return result; },但即使它是更好的代码,也没有解决我的问题
  • 您是否收到任何错误,(与 messagebodywriter 相关),您也可以发送一个简单的字符串作为 application/json 吗? , 也许你可以发布你的 web.xml

标签: java web-services rest jersey jax-rs


【解决方案1】:

我找到了使用genson 的解决方案。

我在我的 pom 文件中添加了以下依赖项:

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

并将我的result 封装在一个响应中:

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

然后瞧,

我希望分享我的问题可能对其他人有所帮助!

【讨论】:

    【解决方案2】:

    您可能希望按如下方式更改您的方法:

    @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>
    

    【讨论】:

    • 我在回答中做了一些细微的修改。如果您在 Tomcat 中运行此程序,您将需要正确的库(如上面我的 pom.xml 中所述),否则您将收到错误 500。该 URL 向我返回一个带有输出 ["one"," 的 getAll.json二","三"].
    • 您好,很抱歉这么久才回复。我已经尝试过你的建议,但遗憾的是我无法让它对我有用。我有以下依赖项:postgresql、mongodb、morphia、glassfish jersey media、glassfish jersey 容器、junit、commons lang,以及你给我的 3 个依赖项。我会将完整的 pom.xml 文件添加到我的问题中:)
    • 我还将添加调用“getAll()”方法时获得的完整日志。
    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多