Reputation: 127
In the Jersey API documentation there is an example for POSTing to a service using a Form to encapsulate form parameters:
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:9998").path("resource");
Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");
MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyJAXBBean.class);
Instead of using a Form object, I want to use a BeanParam, the same one that is being passed into my method (i.e. my method is just acting as a proxy and re-posting to another service). So something like:
@POST
@Path("/CallService")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response callService(@BeanParam final MyBean requestBean) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:9998").path("resource");
MyJAXBBean bean =
target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(requestBean,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyJAXBBean.class);
}
When I call this endpoint, I get a MessageBodyProviderNotFoundException
:
javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/x-www-form-urlencoded, type=class MyBean, genericType=class MyBean.
MyBean is just a pojo annotated with @XmlRootElement
and @XmlAccessorType(XmlAccessType.FIELD)
and then some fields with @FormParam("company")
.
Looks like when the post is being created it doesn't then invoke the provider for type application/x-www-form-urlencoded
...
Upvotes: 0
Views: 2385
Reputation: 208974
"Instead of using a Form object, I want to use a BeanParam"
You can't. Just stick to using the Form
. The @BeanParam
is mean strictly for the server side, and it is not even for just form params, it is for all other params also. The point is to combine them on the server side for easy access.
When you try to send the bean on the client. The client looks for a MessageBodyWriter
that can handle application/x-www-form-urlencoded
and MyBean
. It will not find one and you will get the error you are currently getting. The MessageBodyWriter
s available for application/x-www-form-urlencoded
can handle Form
and MultivaluedMap
[1].
If you really want to send the data as a bean, then send it as application/json
. Other than that, you are stuck with using Form
or MultivaluedMap
[1] - See
Upvotes: 1