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?