I am getting following error(406) while using CXF webclient, but when I use URL api, i am getting expected output.
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)
My client code is:
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 endpoint Service class:
@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 {
Question
1) What changes I need to make, to fix the error
2) To return as Json, what changes I need to make? If I write, conn.setRequestProperty("Accept", "application/xml");I am getting error.
Found solution:
client side changes:
WebClient webClient = WebClient.create("http://localhost:8080/assignment/cxf/login/test/test1");
String res = webClient.accept("application/xml").get(String.class);
here instead of String res = webClient.accept("text/xml").get(String.class);
made
String res = webClient.accept("application/xml").get(String.class);
Service class changes:
@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)
changed MediaType.APPLICATION_XML
in userLogin method instead of MediaType.TEXT_XML
According to the HTTP spec, a 406 response means that the server cannot meet the requirements as set out in the request's ACCEPT headers. However, the server-side log messages seem to be implying that something else is happening.
I suggest that you do what the log message suggests. Enable debug logging and see what the fine-grained log messages say about what is going wrong.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.