john
john

Reputation: 11699

How to pass raw JSON as a response from Restful service?

I have a RestService in which I am returning a JSON response as shown below.

Below is my code:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@GET
@Path("/json/lime")
@Produces(MediaType.APPLICATION_JSON)
public DataResponse getData(@Context DataInfo info) {

    // some code here
    System.out.println(resp.getResponse());
    return new DataResponse(resp.getResponse());
}

Below is the actual JSON response I get back after hitting my Restful Service:

{
    "response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}

And this is what it gets printed out from System.out as shown above:

{"ABC":100,"PQR":"40"}

Here is my DataResponse class:

public class DataResponse {

    private String response;

    public DataResponse(String response) {
        this.response = response;
    }

    public String getResponse() {
        return this.response;
    }
}

As you can see, my response string is getting escaped in the above JSON. And I am not sure why? Can anyone help me understand why this is happening and how to fix it?

Upvotes: 1

Views: 2446

Answers (3)

Yashar
Yashar

Reputation: 133

Use Response class:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

    @GET
    @Path("/json/lime")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponsegetData(@Context DataInfo info) {
        ...
        return Response.status(200).entity(resp.getResponse()).build();
    }

Upvotes: 2

brso05
brso05

Reputation: 13232

The problem is you are trying to return a JSON string as the response. Let the webservice build the JSON for you like so (don't build the JSON string yourself):

public class DataResponse {

    private int ABC;
    private String PQR;

    public DataResponse()
    {

    }

    public DataResponse(int ABC, String PQR) {
        this.ABC = 100;
        this.PQR = "40";
    }
    //getters and setters here
}

The above is just an example I don't expect you to hard code values.

The resulting JSON of the above should be something like:

{
    "ABC":100,
    "PQR":"40"
}

The program is working as expected it is changing your DataResponse class into JSON but since you have JSON stored in the string that it is converting it is escaping characters in Javascript.

{
    "response": "{\"ABC\":100,\"PQR\":\"40\"}\n",
}

Your javascript object will be like this:

window.alert(data.response);

Should print:

{"ABC":100,"PQR":"40"} //same as system.out.print

I hope you understand what I am saying if not just ask...

***************************************************UPDATE********************************************************

@GET
@Path("/json/lime")
@Produces(MediaType.TEXT_PLAIN)
public String getData(@Context DataInfo info) {

    // some code here
    System.out.println(resp.getResponse());
    return resp.getResponse();
}

Javascript side convert String to Object example:

var test = JSON.parse("{\"ABC\":100,\"PQR\":\"40\"}");
window.alert(test.ABC);
window.alert(test.PQR);

String to JSON reference here

Upvotes: 1

Sasikanth Bharadwaj
Sasikanth Bharadwaj

Reputation: 1457

Your response contains double quotes in it. Without escaping them, the value wouldn't be parsable, for example, your string would look like "{"ABC":100,"PQR":"40"}\n", which isn't a valid string value at all. To include any character that has special meaning in a string, it has to be escaped.

Upvotes: 0

Related Questions