泽西岛客户可以使用JSON将JAXB对象发布到服务器吗? [英] Can jersey clients POST a JAXB object to the server using JSON?

查看:128
本文介绍了泽西岛客户可以使用JSON将JAXB对象发布到服务器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在找到许多有关如何设置jersey服务器以使其能够产生和使用JAXB绑定对象的示例,但是我在寻找如何使客户端发布相同的JAXB绑定对象的示例时遇到了麻烦. 此示例显示了如何使用XML .我正在寻找一个显示如何使用JSON的示例.

I'm finding a lot of examples of how to set up a jersey server so that it can produce and consume JAXB bound objects but I'm having trouble finding examples of how to get the client to post the same JAXB bound object. This example shows how to do it with XML. I'm looking for one that shows how to do it with JSON.

我什至不确定是否可以这样做.

I'm not even sure if this is possible to do. The javadoc on the post method(s) are ambiguous.

我的帖子如下:

    Client client = Client.create();
    WebResource resource = client.resource(uri);
    ClientResponse response = resource.type(MediaType.APPLICATION_JSON)
      .post(ClientResponse.class, instanceWithXmlRootElementAnnotation);

当我尝试此操作时,我的服务器会收到请求,但@FormParam的字段始终以null形式发送.这是我的服务器端方法的签名:

When I try this, my server gets the request, but the field for the @FormParam is always sent over as null. Here's the signature of my server side method:

@POST
@Path("apath")
@Consumes(MediaType.APPLICATION_JSON)
public String postAPath(@FormParam("InstanceWithXmlRootElementAnnotation")
  InstanceWithXmlRootElementAnnotation instanceWithXmlRootElementAnnotation) {
//instanceWithXmlRootElementAnnotation is always null

我想知道的是我是否应该使用instanceWithXmlRootElementAnnotation.如果这是传统的Web服务,我将使用JAXB generate (生成)对象供客户端使用并通过生成的类进行发送.但是从我链接到的示例中收集的信息来看,这个家伙正在发送源代码,而不是生成的类.

Something else I'm wondering is if I should be using instanceWithXmlRootElementAnnotation. If this were a traditional webservice, I would use JAXB to generate an object for the client to use and send over the generated class. But from what I gather from the example I linked to, the guy is sending over the source, not the generated class.

推荐答案

我自己弄清楚了.问题是服务器端的问题.删除服务器上的@FormParam批注后,所有操作均按预期进行.结合在一起的问题和答案将为其他人提供基础教程.我的服务器现在看起来像:

I figured it out on my own. The problem was a server side issue. Once I removed the @FormParam annotation on the server, everything worked as expected. The combined question and answer will provide a rudimentary tutorial for others. My server now looks like:

@POST
@Path("apath")
@Consumes(MediaType.APPLICATION_JSON)
public String postAPath(InstanceWithXmlRootElementAnnotation instanceWithXmlRootElementAnnotation) {

这篇关于泽西岛客户可以使用JSON将JAXB对象发布到服务器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆