2016-03-22 7 views
8

में प्रतिक्रिया निकाय कैसे सेट करें एक आरईएसटी एपीआई एंडपॉइंट है जिसे कार्यान्वित करने की आवश्यकता है, कुछ जानकारी प्राप्त करने के लिए उपयोग किया जाता है और बैकएंड सर्वर से आने वाले प्रतिक्रिया को एक अन्य सर्वर और प्रतिक्रिया भेजता है अंतिम प्रतिक्रिया को सेट करना है। मेरी समस्या javax.ws.rs.core. प्रतिक्रिया प्रतिक्रिया में प्रतिक्रिया निकाय कैसे सेट करें?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); 
    } 

} 

यहाँ संदेश एक मैं निर्धारित करने की आवश्यकता है। लेकिन मैंने कई तरीकों की कोशिश की। उनमें से कोई काम नहीं किया।

+2

'वापसी Response.ok (संदेश) .build()'? –

उत्तर

14

निम्न समाधानों चाल करना चाहिए में से एक:

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

अधिक जानकारी के लिए Response और Response.ResponseBuilder कक्षाएं प्रलेखन पर एक नजर है।

टिप: Response.ResponseBuilder एपीआई में आप कुछ उपयोगी तरीकों कि आप HTTP प्रतिसाद करने के लिए cache, cookies और headers से संबंधित जानकारी जोड़ सकते हैं हो सकता है।

+1

हां। 'वापसी Response.ok (संदेश) .build() 'मेरे लिए काम करता है। इसके अलावा 'वापसी Response.ok() इकाई (संदेश) .build() 'काम नहीं किया। वैसे भी धन्यवाद और Acceped उत्तर – GPrathap

+0

Response.ok (संदेश) .build() 200 वापस करेगा, क्या हम कुछ भी BAD_REQUESTs के साथ कुछ भी कर सकते हैं। मेरा मतलब है एक कस्टम संदेश जोड़ना। – prime

+0

@ प्राइम 'रिटर्न Response.status (Response.Status.BAD_REQUEST) .entity (इकाई) .build();' –

संबंधित मुद्दे

 संबंधित मुद्दे