简体   繁体   English

如何在javax.ws.rs.core.Response中设置Response body

[英]How set Response body in 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. 需要实现的REST API端点用于获取一些信息并将后端请求发送到另一个服务器,来自后端服务器的响应必须设置为最终响应。 My problem is how to set response body in javax.ws.rs.core.Response? 我的问题是如何在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();

For more details, have a look at Response and Response.ResponseBuilder classes documentation. 有关更多详细信息,请查看ResponseResponse.ResponseBuilder类文档。

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. 提示 :在Response.ResponseBuilder API中,您可能会发现一些有用的方法,允许您将与缓存cookie标头相关的信息添加到HTTP响应中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpServletResponse 到 javax.ws.rs.core.Response - HttpServletResponse to javax.ws.rs.core.Response 如何解析 javax.ws.rs.core.Response - How to Parse javax.ws.rs.core.Response 如何在Java中将哈希集添加到javax.ws.rs.core.Response - how to add a Hashset to a javax.ws.rs.core.Response in java 如何从 javax.ws.rs.core.Response 响应中检索 JSON 响应? - How to retrieve JSON Response from a javax.ws.rs.core.Response response? 使用 javax.ws.rs.core.Response 和断言的模拟方法 - Mocking method with javax.ws.rs.core.Response and assertion 返回javax.ws.rs.core.Response的列表,结果为500 - Returning a list of javax.ws.rs.core.Response resulting in 500 “javax.ws.rs.core.Response”依赖项的构建失败 - Build failures for 'javax.ws.rs.core.Response' dependency 泽西岛:如何将MediaType设置为javax.ws.rs.core.Response,如果实际的“物理”响应头没有任何相关信息 - Jersey: how to set MediaType to javax.ws.rs.core.Response, if real “physical” response header does not have any information about it 以集合为实体的javax.ws.rs.core.Response - javax.ws.rs.core.Response with a collection as entity 如何从JAX-RS javax.ws.rs.core.Response获取原始请求的URI - How to get the original requested URI from JAX-RS javax.ws.rs.core.Response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM