在Jersey API文档中,有一个使用Form来封装表单参数的服务POST示例:
apply
我不想使用Form对象,而是使用BeanParam,这是传递给我的方法的那个(即我的方法只是作为代理并重新发布到另一个服务)。如下所示:
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);
当我调用此端点时,我得到@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
MyBean只是一个用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.
和@XmlRootElement
注释的pojo,然后带有@XmlAccessorType(XmlAccessType.FIELD)
的一些字段。
看起来在创建帖子时,它不会调用类型为@FormParam("company")
的提供者...
答案 0 :(得分:1)
“我不想使用Form对象,而是想使用BeanParam”
你做不到。坚持使用Form
。 @BeanParam
严格意味着服务器端,它甚至不仅仅是形式参数,它也适用于所有其他参数。重点是将它们组合在服务器端以便于访问。
当您尝试在客户端上发送bean时。客户端会查找可以处理MessageBodyWriter
和application/x-www-form-urlencoded
的{{1}}。它找不到一个,你会得到你目前得到的错误。 MyBean
可用的MessageBodyWriter
可以处理application/x-www-form-urlencoded
和Form
[ 1 ] 。
如果您确实希望将数据作为bean发送,请将其作为MultivaluedMap
发送。除此之外,您仍然无法使用application/json
或Form
[ 1 ] - 见