2016-03-22 16 views
8

Uygulanması gereken bir REST API son noktası, bazı bilgileri almak ve arka uç sunucusundan gelen başka bir sunucuya ve yanıt için arka uç isteği göndermek için kullanılır. son cevabı ayarlamanız gerekiyor. Benim sorunum javax.ws.rs.core.Response içinde yanıt gövdesi nasıl ayarlanır?javax.ws.rs.core.Response içindeki yanıt gövdesi nasıl ayarlanır?

@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); 
    } 

} 

İşte mesajı Ben ayarlamanız gerekir biridir. Ama birkaç yöntem denedim. Hiçbiri işe yaramadı. hile yapmak gerekir aşağıdaki çözümlerden

+2

'dönüş Response.ok (ileti) .build()'? –

cevap

14

Bir: Daha fazla ayrıntı için

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

, Response ve Response.ResponseBuilder sınıflar belgelerine bir göz.

İpucu: Eğer HTTP yanıtı cache, cookies ve headers ilgili bilgi eklemek için izin bazı faydalı yöntemler bulabilir Response.ResponseBuilder API olarak.

+1

Evet. Response.ok (message) .build() 'dönüşü benim için çalışıyor. Ayrıca 'Response.ok() .ity (message) .build()' dönüşü işe yaramadı. Her neyse teşekkürler ve acceped cevap – GPrathap

+0

Response.ok (mesaj) .build() bir 200 dönecektir, biz de BAD_REQUESTs aynı şey yapabiliriz. Özel bir mesaj eklemek istiyorum. – prime

+0

@prime 'ResponseStatus (Response.Status.BAD_REQUEST) .entity (varlık) .build();' –

İlgili konular