【问题标题】:How to send http request parameters using Jersey client如何使用 Jersey 客户端发送 http 请求参数
【发布时间】:2016-07-12 08:53:58
【问题描述】:

我使用 jersey 的以下 REST 客户端实现来使用 REST 服务。我能够成功地做到这一点。此外,现在我需要发送请求参数,这些参数将在生产者端作为 HttpServletRequest 的一部分使用。

消费者端 Jersey 客户端代码

    private ClientResponse getWebClientResponse(String RESOURCE_PATH, String methodType, Object requestObj) {
    WebResource webResource;
    ClientResponse response = null;
    try {
        String environmentHost = EnvironmentUtil.resolveEnvironmentHost();
        Client client = prepareClient();
        String RWP_BASE_URI = environmentHost + "/workflow/rest";
        webResource = client.resource(RWP_BASE_URI);
        WebResource path = webResource.path(RESOURCE_PATH);
        if (GET.equals(methodType)) {
            response = path.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(
                ClientResponse.class);
        } else if (POST.equalsIgnoreCase(methodType)) {
            response = path.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(ClientResponse.class, requestObj);
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return response;
}

制作方

@Context
public void setContext(SecurityContext context) {
    this.context = context;
}

public HttpServletRequest getRequest() {
    return request;
}

@Context
public void setRequest(HttpServletRequest request) {
    this.request = request;
}

public String getSessionUserPID(final HttpServletRequest request,
                                final SecurityContext context) {
    if (request.getSession(false) == null) {
        final String exceptionMessage = "getSessionUserPID() failed, session NOT FOUND for this request";
        final Response response = Response.status(ExceptionStatus.UNAUTHORIZED.getNumber())
                .entity(exceptionMessage).build();
        LOG.error(exceptionMessage);
        throw new WebApplicationException(response);
    }

    if (context.getUserPrincipal() == null) {
        final String exceptionMessage = "getSessionUserPID() failed, user principal NOT FOUND";
        final Response response = Response.status(ExceptionStatus.UNAUTHORIZED.getNumber())
                .entity(exceptionMessage).build();
        LOG.error(exceptionMessage);
        throw new WebApplicationException(response);
    }

    final String userPID = context.getUserPrincipal().getName();
    if (userPID == null || userPID.isEmpty()) {
        final String exceptionMessage = "getSessionUserPID() failed, user principal name cannot be null or empty";
        final Response response = Response.status(ExceptionStatus.UNAUTHORIZED.getNumber())
                .entity(exceptionMessage).build();
        LOG.error(exceptionMessage);
        throw new WebApplicationException(response);
    }

    return userPID;
}

这里的主要意图是目前我从 weblogic 安全上下文中获取用户信息,但是对于特定场景,我需要传递这部分 rest 服务请求并从 HttpServletRequest 对象中获取它。我如何从 httpservletrequest 中获取它

【问题讨论】:

    标签: http jersey


    【解决方案1】:

    您可以在GET方法中使用QueryParam或PathParam,在POST方法中使用FormParam向服务器发送请求参数。

    【讨论】:

    • 你没有正确理解我的问题。我不希望信息作为路径参数传递。在服务器端,我想从 httpservlet 请求对象获取信息。如果我将它添加到查询参数或路径参数中,那么我的消费者服务签名将会改变。我不应该打扰它,因此我试图传递一个参数并通过消费者端的 httpservlet 请求对象读取它
    • 然后在HttpHeader中加入需要的参数
    猜你喜欢
    • 1970-01-01
    • 2020-11-28
    • 2015-04-26
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 2020-09-02
    相关资源
    最近更新 更多