1

我正在拼命寻找一种方法来使用根元素进行 JSON 序列化,以便在启用了 RestEasy 和 Jettison 提供程序的 JBoss AS 7.1 上工作。

尽管根据 RestEasy 文档,返回 JSON 根元素应该可以工作,但在请求 REST servlet 时我从不检索。

我使用对象工厂:

@XmlRegistry
public class ObjectFactory {

    private final static QName _NotificationList_QNAME = new QName("xxx:xxx:xxx", "notificationList");

   public NotificationList createNotificationList() {
        return new NotificationList();
    }

    @XmlElementDecl(namespace = "xxx:xxx:xxx", name = "notificationList")
    public JAXBElement<NotificationList> createNotificationList(NotificationList value) {
        return new JAXBElement<NotificationList>(_NotificationList_QNAME, NotificationList.class, null, value);
    }

}

使用以下 XML 对象:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "NotificationList", namespace = "xxx:xxx:xxx:xxx", propOrder = {
    "any"
})
@XmlRootElement (name = "notificationList" )
public class NotificationList {

    @XmlAnyElement(lax = true)
    protected List<Object> any;


    @XmlElement (name="notificationList")
    public List<Object> getAny() {
        if (any == null) {
            any = new ArrayList<Object>();
        }
        return this.any;
    }
}

我希望与根元素“notificationList”一起返回通知列表,但我不明白。我默认使用 Jettison 提供程序,但也切换到 Jackson。两者都不适合我。

也许值得一提的是,REST 方法并没有返回对象本身,而是将 AsynchronousResponse 传递给另一个对象,该对象处理并最终将 JSON 对象返回给我在创建响应时使用 AsynchronousResponse

编辑:有关实际使用 NotificationList 的类的更多信息:

以下 REST 类使用 NotificationChannel 类(此处不感兴趣)并将异步响应对象传递给另一个类。此响应对象最终返回 NotificationList。以简化的方式,如下所示:

     @Path("/notificationchannel/v1")
            public class NotificationChannelService {

        @POST
        @Path("/{userid}/channels")
        @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
        @Mapped(namespaceMap = {
           @XmlNsMap(namespace = "XXXX:XXXX:XXXX:XXXX", jsonName = "notificationChannel")
               })

        public void createNotificationChannel(
            final @Suspend(10000) AsynchronousResponse response,
            final JAXBElement<NotificationChannel> ncParam, 
            @PathParam("userid") String userID) {


            NotificationManager nMan = new NotificationManager(resp);

        }
 }

响应创建并返回如下:

   public class NotificationManager {

      public NotificationManater(AsynchronousResponse resp){

      //dostuff


      notificationList = objectFatory.creatNotificationList();

      //add notification object (also defined int ObjectFactory)
       notificaitonList.addObject(messageNotification)
       notificaitonList.addObject(statusNotification)
       notificaitonList.addObject(inviteNotification)

       //dostuff


       resp.setResponse(Response.created(UriBuilder.fromUri(nc.getResourceURL()).build())
                        .entity(notificationList)
                        .type(MediaType.APPLICATION_JSON)


                    .build());
        }
     }

在客户端,我希望得到以下响应:

{"notificationList": {
    "inboundMessageNotification": {"inboundMessage": {
        "destinationAddress": "tel:+19585550100",
        "inboundMMSMessage": {"subject": "Who is RESTing on the beach?"},
        "link": {
            "href": "http://example.com/exampleAPI/v1/messaging/inbound/subscriptions/sub123",
            "rel": "Subscription"
        },
        "messageId": "msg123",
        "resourceURL": "http://example.com/exampleAPI/v1/messaging/inbound/registrations/reg123/messages/msg123      ",
        "senderAddress": "tel:+19585550101"
    }},
    "presenceNotification": {
        "callbackData": "1234",
        "link": {
            "href": "http://example.com/exampleAPI/v1/presence/tel%3A%2B19585550101/subscriptions/presenceSubscriptions/        tel%3A%2B19585550100/sub001",
            "rel": "PresenceSubscription"
        },
        "presence": {"person": {"mood": {"moodValue": "Happy"}}},
        "presentityUserId": "tel:+19585550100",
        "resourceStatus": "Active"
    }
}} 

但我确实得到了这个(没有 RootElement 名称,没有通知对象名称):

{
    {
        "destinationAddress": "tel:+19585550100",
        "inboundMMSMessage": {"subject": "Who is RESTing on the beach?"},
        "link": {
            "href": "http://example.com/exampleAPI/v1/messaging/inbound/subscriptions/sub123",
            "rel": "Subscription"
        },
        "messageId": "msg123",
        "resourceURL": "http://example.com/exampleAPI/v1/messaging/inbound/registrations/reg123/messages/msg123      ",
        "senderAddress": "tel:+19585550101"
    },
    {
        "callbackData": "1234",
        "link": {
            "href": "http://example.com/exampleAPI/v1/presence/tel%3A%2B19585550101/subscriptions/presenceSubscriptions/        tel%3A%2B19585550100/sub001",
            "rel": "PresenceSubscription"
        },
        "presence": {"person": {"mood": {"moodValue": "Happy"}}},
        "presentityUserId": "tel:+19585550100",
        "resourceStatus": "Active"
    }
} 
4

1 回答 1

1

我遇到了完全相同的问题,问题是基于 Jackson 的 json 提供程序(resteasy-jackson-provider)实际上正在接管序列化(由于隐式模块依赖性)。我所做的是使用具有以下内容的特定部署描述符META-INF/jboss-deployment-structure.xml

 <jboss-deployment-structure>
  <deployment>
    <exclusions>
      <module name="org.jboss.resteasy.resteasy-jackson-provider" />
    </exclusions>
    <dependencies>
      <module name="org.jboss.resteasy.resteasy-jettison-provider" />
    </dependencies>
  </deployment>
</jboss-deployment-structure>

它将强制容器为您的应用程序切换到 jettison 提供程序。

于 2013-12-05T18:50:45.820 回答