Jersey UniformInterfaceException试图代理到REST POST服务 [英] Jersey UniformInterfaceException trying to proxy to REST POST service

查看:169
本文介绍了Jersey UniformInterfaceException试图代理到REST POST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试执行以这种方式构建的代码时,我一直收到一个406 HTTP响应。我已经尝试过很多次重构代码和输入,但是我仍然收到这个错误,而且我甚至不知道要调试什么。这个异常似乎表明 post()方法没有提供所需格式的 @FormParam 但您可以看到 .accept(MediaType.APPLICATION_FORM_URLENCODED) @Consumes(MediaType.APPLICATION_FORM_URLENCODED)匹配。



我使用Firefox附加组件 HTTPRequester 传入 @FormParam s,并确保我将它们传入适当的Content-Type(应用程序/ x-WWW窗体-urlencoded )。我已经用完了检查的东西。有没有人有任何想法?






代理服务

 客户端客户端= Client.create(); 
WebResource服务= 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)字符串value1,
@FormParam(value2)字符串value2,
@FormParam(valueN)字符串valueN){

String returnValue = null;

/ *
*做东西
* /

return returnValue;

  com.sun.jersey.api.client.UniformInterfaceException:POST http:// theURL / theService在com返回的响应状态为406 
。 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)


解决方案

UniformInterfaceException 只是一个不好名称的全部异常(它被命名为这是因为它是一个提供统一接口的异常,不管错误)。这基本上是Jersey中任何东西抛出的IOException。实际的错误是 406无法接受


所请求的资源只能根据请求中发送的Accept标头生成不可接受的内容。


这里您表示接受 MediaType.APPLICATION_FORM_URLENCODED

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

但是您的服务产生 MediaType.APPLICATION_XML

  @Produces(MediaType.APPLICATION_XML)

因为你的服务器不能产生任何客户说它会接受的内容,所以它会返回一个406错误。



,你的意思是设置 WebResource.type ,而不是接受

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


I keep receiving a 406 HTTP response when I try to execute code structured in this way. I have tried restructuring the code and the inputs many times, but I still receive this error, and I've gotten to the point I don't even really know what to debug. The exception seems to indicate that the post() method isn't supplying the @FormParams in the desired format, but as you can see the .accept(MediaType.APPLICATION_FORM_URLENCODED) and the @Consumes(MediaType.APPLICATION_FORM_URLENCODED) do indeed match up.

I am using the Firefox add-on HTTPRequester to pass in the @FormParams and have ensured that I am passing them in with the appropriate Content-Type (application/x-www-form-urlencoded). I've run out of things to check. Does anyone have any ideas?


The Proxy Service

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

The Actual Service

@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;
}

The Exception

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)

解决方案

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试图代理到REST POST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆