0

I make a call to a server using Java in order to receive json information and I want to retrieve that information through a Rest Web Servie. Right now I have this code:

public class consult {
  public static void consult1() the url that retrieves json);
    InputStream response = url.openStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(response));
        for (String line; (line = reader.readLine()) != null;) {
                System.out.println(line);

            }
    reader.close();
}

This shows the Json perfectly on the console but how can I set the information into some variable so when I call this WS in my browser (/resources/consult/) shows the info according to this WS?

@Path("/consult")
public class ConsultJSON {

@GET
@Produces ("application/json")
public String getInfo() throws MalformedURLException, IOException {
   return consult.consult1();
}
0

2 Answers 2

1

I suggest you checking out this tutorial on how to implement RESTful JSON service:

http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/

The idea behind the proper setup is that you don't need to return serialized string from your method. You just return a domain object and instruct framework to serialize it through annotations.

And of course you'll find there examples on how to POST new objects or updates for it (in JSON).

Sign up to request clarification or add additional context in comments.

1 Comment

But I'm going to receive much more json information through this web service, I don't want to implement a Bean for each consult. I just want to return the json I already have.
0

In reality you really should pipe the InputStream from the response directly to your OutputStream. There are facilities for doing this piping automatically in TUS in IOUtils / DataFetcher: http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.