泽西UniformInterfaceException试图代理REST服务POST(Jersey U

2019-09-22 15:07发布

我不断收到一个406 HTTP响应,当我尝试执行这样构造的代码。 我曾尝试重组代码并投入了很多次,但我仍然收到此错误,我已经得到了我并不真正了解什么调试点。 唯一的例外似乎表明,在post()方法不供应@FormParam所需格式S,但你可以看到.accept(MediaType.APPLICATION_FORM_URLENCODED)@Consumes(MediaType.APPLICATION_FORM_URLENCODED)确实匹配。

我使用的Firefox插件HTTPRequester在通过@FormParam S和确保了我在通过它们与相应的内容类型( application/x-www-form-urlencoded )。 我的东西,检查用完。 有没有人有什么想法?


代理服务

Client client = Client.create();
WebResource service = client.resource(myURL);

Form form = new Form();
form.add("value1", value1);
form.add("value2", value2);
form.add("valueN", valueN);

String returnValue = service.accept(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form);

实际的服务

@POST
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Path("/theService")
public String theService(
        @FormParam("value1") String value1,
        @FormParam("value2") String value2,
        @FormParam("valueN") String valueN) {

    String returnValue = null;

    /*
     * Do Stuff
     */

    return returnValue;
}

例外

com.sun.jersey.api.client.UniformInterfaceException: POST http://theURL/theService returned a response status of 406
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:563)
at com.sun.jersey.api.client.WebResource.access$300(WebResource.java:69)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:499)

Answer 1:

UniformInterfaceException is just a catch-all exception with a poor name (it's named this because it's an exception that provides a uniform interface, no matter the error). It's basically an IOException thrown by anything in Jersey. The actual error is the 406 Unacceptable:

The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.

Here you're saying that you accept MediaType.APPLICATION_FORM_URLENCODED:

String returnValue = service.accept(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form);

But your service produces MediaType.APPLICATION_XML:

@Produces(MediaType.APPLICATION_XML)

Since your server can't produce any content that the client says it will accept, it returns a 406 error.

Most likely, you're meaning to set WebResource.type, not accept:

String returnValue = service.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class, form);


文章来源: Jersey UniformInterfaceException trying to proxy to REST POST service