0

I am serializing a String Buffer which I am further converting into a JSON Array. Note that, I am 100% sure that the JSONArray is valid.

Now when I send this JSONArray as a response, the server returns a 500 Request Failed. I am making sure to send this JSONArray as a list.

For example, The string buffer is fully loaded now.

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

I have these dependencies in my 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>

What am I doing wrong here? The server doesn't print any stack traces either.

ayrusme
  • 506
  • 5
  • 17

1 Answers1

0

Well, I still didn't know what I was doing wrong. So I stopped creating the StringBuffer altogether and mapped a POJO into JsonObject. When you return a list of jsonobjects, Grizzly will automatically map them into a JSONArray, if you have the getters, setters and a constructor specified. Also, make sure to use the @JsonProperty annotation.

Example for reference:

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

This correctly parses to/from the JSON:

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

Code taken from: When is the @JsonProperty property used and what is it used for? Thanks for the code, @OldCurmudgeon :)

ayrusme
  • 506
  • 5
  • 17