2013-08-03 95 views
0

我在使用CXF webclient時遇到以下錯誤(406),但是當我使用URL API時,我得到了預期的輸出。cxf webclient - 異常[請求處理失敗;嵌套的異常是狀態:406

INFO: Reloading Context with name [/assignment] is completed 
http://localhost:8080/assignment/cxf/login/test/test1 
Result--><?xml version="1.0" encoding="UTF-8" standalone="yes"?><user> <userName>test</userName> <firstName>Mike</firstName> <lastName>Tom</lastName></user> 
Aug 02, 2013 11:20:31 PM org.apache.cxf.jaxrs.utils.JAXRSUtils findTargetMethod 
WARNING: No operation matching request path "/assignment/cxf/login/test/test1" is found, Relative Path: /login/test/test1, HTTP Method: GET, ContentType: */*, Accept: application/xml,. Please enable FINE/TRACE log level for more details. 
Aug 02, 2013 11:20:31 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse 
WARNING: WebApplicationException has been caught : no cause is available 
Aug 02, 2013 11:20:31 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/assignment] threw exception [Request processing failed; nested exception is Status : 406 
Headers : 
Date : Sat, 03 Aug 2013 04:20:31 GMT 
Content-Length : 0 
Server : Apache-Coyote/1.1 
] with root cause 
Status : 406 
Headers : 
Date : Sat, 03 Aug 2013 04:20:31 GMT 
Content-Length : 0 
Server : Apache-Coyote/1.1 

    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:680) 
    at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:324) 
    at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:421) 
    at com.viasat.test.login.servlet.LoginServlet.processLogin(LoginServlet.java:45) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 

我的客戶端代碼:

 URL url = new URL("http://localhost:8080/assignment/cxf/login/"+name+"/"+password); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setRequestProperty("Accept", "application/json"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream()))); 
     StringBuilder res = new StringBuilder(); 
     String output; 
     while ((output = br.readLine()) != null) { 
      res.append(output); 
     } 
     System.out.println("Result-->"+res.toString()); 
     WebClient webClient = WebClient.create("http://localhost:8080/assignment/cxf/login/"+name+"/"+password);    
    // WebClient t = webClient.path(""); 
     String res = webClient.accept("text/xml").get(String.class); 
     System.out.println("=============="+res); 

REST端點服務類:

@GET 
    @Path("/login/{userName}/{password}") 
    @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML }) 
    public String userLogin(@PathParam("userName")String username, @PathParam("password")String password) 
      throws JAXBException, PropertyException, FileNotFoundException { 

問題

1)什麼改變,我需要做,修復錯誤

2)作爲Json返回,我需要做什麼更改?如果我寫,conn.setRequestProperty(「Accept」,「application/xml」);我得到錯誤。

回答

1

實測溶液:

客戶端更改:

WebClient webClient = WebClient.create("http://localhost:8080/assignment/cxf/login/test/test1");    
String res = webClient.accept("application/xml").get(String.class); 

這裏代替String res = webClient.accept("text/xml").get(String.class);製成

String res = webClient.accept("application/xml").get(String.class); 

服務類的變化:

@GET 
    @Path("/login/{userName}/{password}") 
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) 
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 
    public String userLogin(@PathParam("userName")String username, @PathParam("password")String password) 

在userLogin方法中改爲MediaType.APPLICATION_XML而不是MediaType.TEXT_XML

0

根據HTTP規範,406響應意味着服務器不能滿足請求的ACCEPT標頭中所述的要求。但是,服務器端日誌消息似乎暗示其他事情正在發生。

我建議你做什麼日誌消息建議。啓用調試日誌記錄並查看細粒度的日誌消息對發生什麼問題的說明。

相關問題