开发者

How to generate customized XML response in Java based web service?

开发者 https://www.devze.com 2023-04-12 21:44 出处:网络
I want to create web service in java using Netbeans 7. I have two entities Product and Category both annotated with JAXB annotations.

I want to create web service in java using Netbeans 7. I have two entities Product and Category both annotated with JAXB annotations. I have created EJBs using these entity classes and web service productservice u开发者_Python百科sing these EJBs. On og the method in webservice class is

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

This method return Product marshaled in XML. One of the element in the XML is categoryId which reference the category it belongs to. I want to generate XML with cat_id replaced by Category objects marshaled form. How can I do this?

SOAP Response for product looks like this:

<?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 Response for category looks like this:

<?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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号