如何在基于Java的Web服务中生成自定义XML响应?

时间:2011-10-11 23:50:04

标签: xml web-services orm jaxb

我想使用Netbeans 7在java中创建Web服务。 我有两个实体产品类别都使用JAXB注释进行注释。 我使用这些EJB使用这些实体类和Web服务 productservice 创建了EJB。 在og webservice类中的方法是

@WebMethod(operationName = "find")
public Product find(@WebParam(name = "id") Long id) {
    Product p = ejbRef.find(id);
    Category c = ejbRef2.find(p.getCategoryId());
    return p;
}

此方法返回以XML格式编组的产品。 XML中的一个元素是 categoryId ,它引用了它所属的类别。我想用cat_id替换为Category对象封送形式来生成XML。我怎么能这样做?

产品的SOAP响应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
    <ns2:findResponse xmlns:ns2="http://services/">
        <return>
            <categoryId>1</categoryId>
            <description>Great for reducing mouse populations</description>
            <id>1</id>
            <imageurl>/images/cat1.gif</imageurl>
            <name>Hairy Cat</name>
        </return>
    </ns2:findResponse>
</S:Body>

类别的SOAP响应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
    <ns2:findResponse xmlns:ns2="http://services/">
        <return>
            <description>Loving and finicky friends</description>
            <id>1</id>
            <imageurl>/images/cats_icon.gif</imageurl>
            <name>Cats</name>
        </return>
    </ns2:findResponse>
</S:Body>
</S:Envelope>

0 个答案:

没有答案