-1

I have the following class:

@Path("/")
public class RESTService {

@GET
@Path("verifica")
@Produces(MediaType.TEXT_PLAIN)
public Response verificaREST(InputStream dadoRecebido) {
    String resultado = "Servico REST startou sucesso";
    return Response.status(200).entity(resultado).build();
}

@Path("multiplica:{n}")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response TimesTwo(@PathParam("n") float n) throws JSONException {

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("primeiro", n);
    jsonObject.put("segundo", 2 * n);
    return Response.status(200).entity(jsonObject.toString()).build();
}

}

When I acess http://localhost:8080/RestWithJSON/123/verificawith this I can see Servico REST startou sucesso . When I enter http://localhost:8080/RestWithJSON/123/multiplica:4 I can see {"primeiro":4,"segundo":8} on my browser. Now I'm trying to use the following client class to receive some JSON from TimesTwo method:

    URL url = new URL("http://localhost:8080/RestWithJSON/123/multiplica:24");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setConnectTimeout(5000);
    connection.setReadTimeout(5000);
    System.out.println(connection.getContentType());
    System.out.println(connection.getInputStream().toString());
    System.out.println(connection.getContent());

But with this I only got the following:

application/json
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@378fd1ac
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@378fd1ac

So, even if my contentType is correct, how can I retrieve my data from JSON?

rado
  • 5,720
  • 5
  • 29
  • 51
  • That looks like code to *send* some JSON in a POST request (though it doesn't work), not to receive a JSON response from a GET. – OrangeDog Nov 22 '16 at 12:34
  • 1
    It looks like you're using Spring for the server, so why not use [Spring for the client too](http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html)? – OrangeDog Nov 22 '16 at 12:35

2 Answers2

1

You should read from the InputStream:

JSONObject myData = new JSONObject(IOUtils.toString(connection.getInputStream(),
    connection.getContentEncoding());

IOUtilsis a class from the Apache Commons IO utility library.

Florian Albrecht
  • 2,266
  • 1
  • 20
  • 25
0

You can access JSON Object in the your client application.

For that you need to use fallowing Jackson Databind dependency:

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.4</version>
        </dependency>

And you need to configure fallowing things in the applicationContext.xml file

<beans:bean -- 

<!-- To  convert JSON to Object and vice versa -->
    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean> 

</beans:bean>
Mahabaleshwar
  • 227
  • 1
  • 13