How set Response body in javax.ws.rs.core.Response

62,612

One of the following solutions should do the trick:

return Response.ok(entity).build();
return Response.ok().entity(entity).build();

For more details, have a look at Response and Response.ResponseBuilder classes documentation.

Tip: In the Response.ResponseBuilder API you might find some useful methods that allow you to add information related to cache, cookies and headers to the HTTP response.

Share:
62,612
GPrathap
Author by

GPrathap

Updated on July 22, 2022

Comments

  • GPrathap
    GPrathap almost 2 years

    There is a REST API endpoint which needs to be implemented is used to get some information and send backend request to an another server and response which is coming from backend server has to set the to final response. My problem is how to set response body in javax.ws.rs.core.Response?

    @Path("analytics")
    @GET
    @Produces("application/json")
    public Response getDeviceStats(@QueryParam("deviceType") String deviceType,
                                   @QueryParam("deviceIdentifier") String deviceIdentifier,
                                   @QueryParam("username") String user, @QueryParam("from") long from,
                                   @QueryParam("to") long to) {
    
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContexts.custom()
                    .loadTrustMaterial(new File(getClientTrustStoretFilePath()), "password## Heading ##".toCharArray(),
                            new TrustSelfSignedStrategy())
                    .build();
        } catch (NoSuchAlgorithmException e) {
            log.error(e);
        } catch (KeyManagementException e) {
            log.error(e);
        } catch (KeyStoreException e) {
            log.error(e);
        } catch (CertificateException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        }
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[] { "TLSv1" },
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        CloseableHttpClient httpclient = HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .build();
        HttpResponse response = null;
        try {
            HttpGet httpget = new HttpGet(URL);
            httpget.setHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
            httpget.addHeader("content-type", "application/json");
            response = httpclient.execute(httpget);
            String message = EntityUtils.toString(response.getEntity(), "UTF-8");
        } catch (ClientProtocolException e) {
            log.error(e);
        } catch (IOException e) {
            log.error(e);
        } 
    
    }  
    

    Here message is the one I need to set. But I tried several methods. Didn't work any of them.

  • GPrathap
    GPrathap about 8 years
    Yes. return Response.ok(message).build() works for me. Also return Response.ok().entity(message).build() didn't works . Anyway thanks and acceped answer
  • prime
    prime about 6 years
    Response.ok(message).build() will return a 200, can we do some thing same to BAD_REQUESTs as well. I mean to add a custom message.
  • cassiomolin
    cassiomolin about 6 years
    @prime return Response.status(Response.Status.BAD_REQUEST).entity(entity).‌​build();