2016-03-22 40 views
8

Esiste un endpoint REST API che deve essere implementato per ottenere alcune informazioni e inviare la richiesta di backend a un altro server e la risposta proveniente dal server di backend deve impostare la risposta finale. Il mio problema è come impostare il corpo della risposta in javax.ws.rs.core.Response?Come impostare il corpo di risposta 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); 
    } 

} 

Qui messaggio è quello che ho bisogno di impostare. Ma ho provato diversi metodi. Non ha funzionato nessuno di loro.

+2

'return Response.ok (message) .build()'? –

risposta

14

Una delle seguenti soluzioni dovrebbe fare il trucco:

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

Per maggiori dettagli, uno sguardo ai documenti Response e Response.ResponseBuilder classi.

Suggerimento: Nella Response.ResponseBuilder API si potrebbe trovare alcuni metodi utili che consentono di aggiungere informazioni relative a cache, cookies e headers alla risposta HTTP.

+1

Sì. 'return Response.ok (message) .build()' funziona per me. Inoltre 'return Response.ok(). Entity (message) .build()' non ha funzionato. Comunque grazie e risposta accettata – GPrathap

+0

Response.ok (message) .build() restituirà un 200, possiamo fare qualcosa di simile a BAD_REQUESTs pure. Voglio dire aggiungere un messaggio personalizzato. – prime

+0

@prime 'return Response.status (Response.Status.BAD_REQUEST) .entity (entity) .build();' –