如何在 javax.ws.rs.core.Response 中设置响应体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36152231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:29:44  来源:igfitidea点击:

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

javaweb-servicesrestjakarta-eejax-rs

提问by GPrathap

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?

有一个需要实现的 REST API 端点用于获取一些信息并将后端请求发送到另一台服务器,来自后端服务器的响应必须设置为最终响应。我的问题是如何在 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 messageis the one I need to set. But I tried several methods. Didn't work any of them.

这里的消息是我需要设置的。但是我尝试了几种方法。没有工作。

采纳答案by cassiomolin

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 Responseand Response.ResponseBuilderclasses documentation.

有关更多详细信息,请查看ResponseResponse.ResponseBuilder类文档。

Tip: In the Response.ResponseBuilderAPI you might find some useful methods that allow you to add information related to cache, cookiesand headersto the HTTP response.

提示:在Response.ResponseBuilderAPI 中,您可能会找到一些有用的方法,这些方法允许您将与缓存cookie标头相关的信息添加到 HTTP 响应中。