返回JSON时Grizzly Server 500 RESPONSE

时间:2017-06-25 13:13:13

标签: java json maven http grizzly

我正在序列化一个字符串缓冲区,我将进一步转换为JSON数组。请注意,我100%确定JSONArray是有效的。

现在,当我发送此JSONArray作为响应时,服务器返回500请求失败。我确保将此JSONArray作为列表​​发送。

例如,字符串缓冲区现在已完全加载。

//Creating the JSONArray
JSONArray jsonArray = new JSONArray(stringBuffer.toString());
//Sending the response
return Response.status(200).entity(jsonArray.toList()).build();

我在pom.xml中有这些依赖项

    <dependency>
         <groupId>org.glassfish.jersey.containers</groupId>
         <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>

        <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.8.2</version>
        </dependency>
        <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.8.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.grizzly/grizzly-http-server -->
        <dependency>
                <groupId>org.glassfish.grizzly</groupId>
                <artifactId>grizzly-http-server</artifactId>
                <version>2.3.29</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-common</artifactId>
            <version>2.22.2</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>

我在这里做错了什么?服务器也不打印任何堆栈跟踪。

1 个答案:

答案 0 :(得分:0)

好吧,我还是不知道自己做错了什么。所以我完全停止创建StringBuffer并将POJO映射到JsonObject。当您返回jsonobjects列表时,如果您指定了getter,setter和构造函数,Grizzly会自动将它们映射到JSONArray。另外,请确保使用@JsonProperty注释。

参考示例:

public class Parameter {
  @JsonProperty("Name")
  public String name;
  @JsonProperty("Value")
  public String value; 
}

这正确解析JSON:

"Parameter":{
  "Name":"Parameter-Name",
  "Value":"Parameter-Value"
}

代码取自:When is the @JsonProperty property used and what is it used for?感谢您的代码,@ OldCurmudgeon:)