UltraSonja
UltraSonja

Reputation: 891

Java: Read in JSON file and expose it in a REST service

I am a front-end web developer trying to learn more about the back-end. Currently I just want to read in a local JSON file and expose it in a REST service to be parsed by AngularJS (does that make sense?). I believe I have the servlet set up correctly, but am not sure about how I am approaching this from a Java perspective. It's worth noting that I'm a JavaScript programmer. There are two methods that I am trying to decide between.

The following methods are contained in the class

@Path("/")
public class JsonRESTService {
    .....
}

First method

@GET
@Path("/jsonService")
@Consumes("application/json")
@Produces("application/json")
public Response jsonREST(InputStream incomingData) {

        StringBuilder jsonBuilder = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
            String line = null;
            while((line = in.readLine()) != null) {
                jsonBuilder.append(line);
            }
        } catch(Exception e) {
            System.out.println("Error Parsing: - ");
        }
        System.out.println("Data Received: " + jsonBuilder.toString());

        return Response.status(200).entity(jsonBuilder.toString()).build();
}

Second Method: not sure what to return.

@GET
@Path("/jsonService")
@Consumes("application/json")
@Produces("application/json")
public Response jsonREST(InputStream incomingData) {

    JSONParser parser = new JSONParser();
    try {
        Object obj = parser.parse(new FileReader("C:/files/flat.json"));
        JSONObject jsonObject = (JSONObject) obj;
    } catch(Exception e) {
        e.printStackTrace();
    }
}

web.xml servlet mapping

<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

So this should be exposed to http://localhost:8080/myapp/rest/jsonService. I got some of this code from a tutorial, but it seems like I want to have a method that returns a JSONObject instead of a Response. Is that correct? Am I on the right track, or is there a really good example of what I am trying to do that I haven't found yet?

Upvotes: 0

Views: 8158

Answers (2)

Keerthivasan
Keerthivasan

Reputation: 12880

There are multiple ways of doing it. You can try this way in the second method

Change the return type to String and return the value of JSONObject as a String using

return jsonObject.toString();

In the client side, Angular JS services - you can convert the String into JSON object through

var obj = JSON.parse(text);

So, now obj is a JSON object which you can use it for further processing.

Upvotes: 1

jayaram S
jayaram S

Reputation: 580

if you are a JavaScript developer and getting started quickly is the goal, then I would recommend you checkout

http://www.dropwizard.io/getting-started.html

There are a few advantages here a) much of the server infrastructure stuff is hidden away b) you can focus on your implementation details c) no need to figure out how to deploy this etc, it comes with a server built in.

To answer your question about the return type - the server will return javax.​ws.​rs.​core.Response - or some other variation of a Response object. This encapsulates things like HTTP Status codes http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html .

Therefore in order to send a simple response back you may use something like :

 return Response.accepted().entity(String.format("{\"JSON\" : \"%s\"}",value)).build();

Replace the string with a JSON string read from file system or generate the JSON from object - what ever your pick of poison is.

Upvotes: 0

Related Questions