需要有关使用Jersey 2.17和moxy来处理XML任何集合的帮助

时间:2015-08-17 16:16:15

标签: jersey moxy jaxb2

Jaxb类(提供,我无法更改它们),

FindResponse.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {

})
@XmlRootElement(name = "findResponse")
public class FindResponse {

    @XmlElement(required = true)
    protected ListWrapper result;

    public ListWrapper getResult() {
        return result;
    }

    public void setResult(ListWrapper value) {
        this.result = value;
    }

}

ListWrapper.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "List", propOrder = {
    "any"
})
public class ListWrapper {

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

    public java.util.List<Object> getAny() {
        if (any == null) {
            any = new ArrayList<Object>();
        }
        return this.any;
    }
}

ListWrapper类中包含的Jaxb类都没有@XmlRoolElement注释(从.xsd,2000 +类生成)。

LagInterface.class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "lag.Interface")
public class LagInterface extends AggregationPortAggregationGroup {

    private String objectFullName;
    private String displayedName;
    private String description;
    private Integer lagId;

    ...
}

Json资源方法

@GET
@Path("json/{samoXmlType}")
@Produces(MediaType.APPLICATION_JSON)
public Response jsonGetService(
        @PathParam("samoXmlType") String samoXmlType,
        @Context HttpServletRequest request,
        @Context HttpServletResponse response)
        throws Exception
{
    LagInterface lag = new LagInterface();
    lag.setObjectFullName("lag-interface:1");
    lag.setDisplayedName("lag 1");
    lag.setDescription("moxy test");
    lag.setLagId(1);

    FindResponse findResponse = new FindResponse();
    ListWrapper result = new ListWrapper();
    result.getAny().addAll(Arrays.asList(lag));
    findResponse.setResult(result);

    return Response.ok(findResponse).build();
}

上述方法的回应:

{"result":{}}

类似的XML响应:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:findResponse xmlns:ns0="xmlapi_1.0"><ns0:result/></ns0:findResponse>

这是与XML Any集合相关的警告消息:

[EL Warning]: XMLAnyCollectionMappingNodeValue: The undefined document root element of a referenced object [XMLDescriptor(LagInterface --> [])] is ignored during marshalling with an any collection|object mapping.

1 个答案:

答案 0 :(得分:0)

好的,我通过使用oxm元数据文件在LagInterface上添加缺少的@XmlRootElement来实现它。但性能非常差,需要一分钟才能获得xml或json内容。

现在我还有另一个问题,在oxm元数据文件中,我必须指定package-name。这意味着我将不得不为我的JAXB模型类创建一些oxm文件。

有没有办法让我的所有模型类都有一个medata文件?