2016-06-02 4 views
0

Apache CXF를 사용하여 웹 API를 작성했습니다. 나는 포스트 방법() HttpServletRequest.getParamter를 사용하는 경우, 그것은 null.Here 코드입니다 반환 : 여기HttpServletRequest.getParamter() CXF에서 null을 반환합니다. Restful service (Post)

@Path("/") 
public class TokenService extends DigiwinBaseService { 

    private static void printRequest(HttpServletRequest httpRequest) { 
     System.out.println("\n\n Headers"); 
     Enumeration headerNames = httpRequest.getHeaderNames(); 
     while (headerNames.hasMoreElements()) { 
      String headerName = (String) headerNames.nextElement(); 
      System.out.println(headerName + " = " + httpRequest.getHeader(headerName)); 
     } 
     System.out.println("\n\n Parameters"); 
     Enumeration params = httpRequest.getParameterNames(); 
     while (params.hasMoreElements()) { 
      String paramName = (String) params.nextElement(); 
      System.out.println(paramName + " = " + httpRequest.getParameter(paramName)); 
     } 
     System.out.println("\n\n Row data"); 
     System.out.println(extractPostRequestBody(httpRequest)); 
    } 

    private static String extractPostRequestBody(HttpServletRequest request) { 
     if ("POST".equalsIgnoreCase(request.getMethod())) { 
      Scanner s = null; 
      try { 
       s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A"); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return s.hasNext() ? s.next() : "null"; 
     } 
     return "null"; 
    } 

    @POST 
    @Consumes("application/x-www-form-urlencoded") 
    public Response Authorize(@FormParam("param") String param, 
      @FormParam("param2") String param2,@Context HttpServletRequest httpRequest) throws OAuthSystemException { 
     printRequest(httpRequest); 
     System.out.println("param:"+param); 
     System.out.println("param2:"+param2);  
     return Response.status(HttpServletResponse.SC_OK).entity("OK").build();  
    } 
} 

하면 테스트 코드 여기

public class HttpClientTest { 

     public static void main(String[] args) throws Exception{ 

      String url4 = "/api/services/Test"; 
      String host = "127.0.0.1"; 
      HttpClient httpClient = new HttpClient(); 
      httpClient.getHostConfiguration().setHost(host, 8080, "http"); 
      HttpMethod method = postMethod(url4); 
      httpClient.executeMethod(method);  
      String response = method.getResponseBodyAsString(); 
      System.out.println(response); 
     } 

     private static HttpMethod postMethod(String url) throws IOException{ 
      PostMethod post = new PostMethod(url); 
      post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk"); 
      NameValuePair[] param = { 
        new NameValuePair("param","param1"), 
        new NameValuePair("param2","param2"),} ; 
      post.setRequestBody(param); 
      post.releaseConnection(); 
      return post; 
     } 
    } 

에게 인쇄 출력입니다 :

Headers 
content-type = application/x-www-form-urlencoded;charset=gbk 
user-agent = Jakarta Commons-HttpClient/3.1 
host = 127.0.0.1:8080 
content-length = 26 

Parameters 

Row data 
null 

param:param1 
param2:param2 

왜 Parameters가 null입니까? 어떻게하면 HttpServletRequest.getParamter()를 사용하여 게시 매개 변수를 얻을 수 있습니까?

+0

하나 만들었 어? – EJP

답변

2

CXF는 FormParams를 채우기 위해 POST 데이터를 소비합니다.

https://issues.apache.org/jira/browse/CXF-2993

해상도는 를 "해결되지 않습니다"입니다. 문제, 그들은 단지 HttpServletRequest의 모든 PARAMS를 복구하거나 사용하기 위해 MultivaluedMap를 사용하는 것이 좋습니다

옵션 1

@POST 
@Consumes("application/x-www-form-urlencoded") 
public Response Authorize(MultivaluedMap<String, String> parameterMap, @Context HttpServletRequest httpRequest) throws OAuthSystemException { 
    //parameterMap has your POST parameters 

왜 전에`releaseConnection`를 호출 옵션 2

@POST 
@Consumes("application/x-www-form-urlencoded") 
public Response Authorize(@Context HttpServletRequest httpRequest) throws OAuthSystemException { 
    //httpRequest.getParameterMap() has your POST parameters