如何在javax.ws.rs.core.Response中设置Response body [英] How set Response body in javax.ws.rs.core.Response

查看:3220
本文介绍了如何在javax.ws.rs.core.Response中设置Response body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要实现的REST API端点用于获取一些信息并将后端请求发送到另一个服务器,来自后端服务器的响应必须设置为最终响应。我的问题是如何在javax.ws.rs.core.Response中设置响应正文?

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.

推荐答案

以下解决方案之一应该可以解决问题:

One of the following solutions should do the trick:

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





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

有关详细信息,请查看 响应 Response.ResponseBuilder 类文档。

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

提示:在 Response.ResponseBuilder 您可能会找到的API一些有用的方法,允许您添加与缓存 cookies 标题到HTTP响应。

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.

这篇关于如何在javax.ws.rs.core.Response中设置Response body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆