有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在javax中设置响应体。ws。rs.core。回答

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

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

}  

这里是我需要设置的消息。但我尝试了几种方法。他们一个都没用


共 (1) 个答案

  1. # 1 楼答案

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

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

    有关更多详细信息,请查看^{}^{}类文档

    提示:在^{}API中,您可能会发现一些有用的方法,这些方法允许您将与cachecookiesheaders相关的信息添加到HTTP响应中