找到你要的答案

Q:Using BeanParam in Jersey Client API

Q:利用API在客户端beanparam球衣

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...

在泽西API文档中,有一个例子,使用表单来封装窗体参数来传递服务:

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

而不是使用一个窗体对象,我想用一个BeanParam,同一个被我的方法(即我的方法只是作为一个代理和重新发布到另一个服务)。所以像:

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

当我打电话给这个端点,我得到一个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只是一个POJO注释”xmlrootelement和@ xmlaccessortype(xmlaccesstype。场)和一些领域的@ FormParam(“公司”)。

就像是创造了它,不然后调用提供程序类型application/x-www-form-urlencoded…

answer1: 回答1:

"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 MessageBodyWriters 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

“而不是使用一个窗体对象,我想用一个BeanParam”

你不能,只是坚持使用表格。“BeanParam完全是为服务器端的意思,它甚至不只是形式的参数,它是所有其他参数也。重点是将它们放在服务器端,便于访问。

当您尝试在客户端发送bean时。客户找messagebodywriter可以处理application/x-www-form-urlencoded,mybean。它不会找到一个,你会得到你目前得到的错误。可用于application/x-www-form-urlencoded的messagebodywriters可以处理形式和multivaluedmap [ 1 ]。

如果你真的想把数据作为一个豆,然后把它为application/json。除此之外,你不得不使用形式或multivaluedmap

[ 1 ]

java  web-services  rest  jersey  jersey-client