我正在拼命寻找一种方法,使用root元素进行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提供商,但也改用杰克逊。两者都不适合我。
也许值得一提的是,REST方法不返回对象本身,而是将AsynchronousResponse传递给另一个对象,该对象处理并最终将JSON对象bacck重新编译为我在创建响应时使用AsynchronousResponse
编辑: 关于实际使用NotificationList的类的更多信息:
以下REST类使用NotificationChannel类(此处不感兴趣)并将Asynchronous Response obejct传递给另一个classe。此响应对象最终返回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"
}
}
答案 0 :(得分:1)
我遇到了完全相同的问题,问题是基于Jackson(resteasy-jackson-provider)的json提供程序实际上正在接管序列化(由于隐式模块依赖性)。我所做的是使用具有以下内容的特定部署描述符 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提供程序。