2016-07-12 153 views
0

我使用下面的休息客戶端實現球衣來使用rest服務。我能夠成功地完成它。此外,我需要發送請求參數,這些參數將作爲生產者端的HttpServletRequest。如何使用Jersey客戶端發送http請求參數

消費側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安全上下文中的用戶信息,但對於特定的情況下,我需要通過這個部分的剩餘服務請求並從HttpServletRequest對象中獲取它。如何從httpservletrequest中獲得此信息

回答

0

您可以使用QueryParam o r GET方法中的PathParam和POST方法中用於向服務器發送請求參數的FormParam。

+0

您沒有正確地得到我的問題。我不希望信息作爲路徑參數傳遞。在服務器端,我想從httpservlet請求對象獲取信息。如果我將它添加到查詢參數或路徑參數中,那麼我的消費者服務簽名將會改變。我不應該打擾它,因此我試圖傳遞一個參數並通過消費端的httpservlet請求對象讀取 – Balaji

+0

然後將所需的參數添加到HttpHeader – Alireza

相關問題