0

我在使用 CXF webclient 时收到以下错误(406),但是当我使用 URL api 时,我得到了预期的输出。

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);

休息端点服务类:

@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");我收到错误消息。

4

2 回答 2

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)

更改MediaType.APPLICATION_XML了 userLogin 方法而不是MediaType.TEXT_XML

于 2013-08-04T22:39:28.350 回答
0

根据 HTTP 规范,406 响应意味着服务器无法满足请求的 ACCEPT 标头中规定的要求。但是,服务器端日志消息似乎暗示正在发生其他事情。

我建议您按照日志消息的建议进行操作。启用调试日志并查看细粒度的日志消息说明出了什么问题。

于 2013-08-03T05:00:28.243 回答