Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey UniformInterfaceException trying to proxy to REST POST service

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)
like image 239
asteri Avatar asked Jan 15 '23 12:01

asteri


1 Answers

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);
like image 187
Brendan Long Avatar answered Jan 31 '23 06:01

Brendan Long