Jersey UniformInterfaceException tries to proxy REST POST service

I keep getting an HTTP 406 response when I try to execute code structured this way. I tried many times to rebuild the code and inputs, but I still get this error, and I realized that I don’t even know what to debug. The exception seems to indicate that the post() method does not provide @FormParam in the desired format, but as you can see .accept(MediaType.APPLICATION_FORM_URLENCODED) and @Consumes(MediaType.APPLICATION_FORM_URLENCODED) do match.

I am using the Firefox HTTPRequester add-in to transmit to @FormParam and ensure that I pass them using the appropriate Content-Type ( application/x-www-form-urlencoded ). I ran out of things to check. Does anyone have any ideas?


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

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

An 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) 
+4
source share
1 answer

UniformInterfaceException is just an exception for everyone with a low name (he called it because it is an exception that provides a single interface, regardless of the error). This is basically an IOException thrown by something in Jersey. Actual 406 error is not acceptable :

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

Here you say that you accept MediaType.APPLICATION_FORM_URLENCODED :

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

But your service creates MediaType.APPLICATION_XML :

 @Produces(MediaType.APPLICATION_XML) 

Since your server cannot create any content that the client will say, it will receive a 406 error.

Most likely you want to set WebResource.type rather than accept :

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

Source: https://habr.com/ru/post/1435140/


All Articles