简体   繁体   中英

JAXB annotation RootElement

I have the following POJO , and the root element is not showing in the xml file (send over SOAP jax-ws call), can people point out the problem please?

@XmlRootElement(name = "CATALOGUE")
@XmlAccessorType( XmlAccessType.FIELD )
public class Catalogue implements Serializable, Comparable<Catalogue>
{

    @XmlTransient
    private Integer catalogueId;

    @XmlElement( name = "COMMENT", required = false, nillable = false )
    private String catalogueComment;

    @XmlElement( name = "VERSION", required = false, nillable = false )
    private String catalogueVersion;

    @XmlElement( name = "VALID_FROM_DTS", required = false, nillable = false )
    private Date catalogueValidFromDts;

    @XmlElement( name = "CREATED_DTS", required = false, nillable = false )
    private Date catalogueCreatedDts;

    @XmlElementWrapper(name = "ITEMLIST")
    @XmlElement(name = "ITEM")
    private List<Item> itemList;

    @XmlTransient
    private Integer catalogueLifecyclePhase;

    @XmlTransient
    private Integer ownerId;

the xml file looks like

            <COMMENT>LK</COMMENT>
            <VERSION>4</VERSION>
            <VALID_FROM_DTS>1990-01-25T00:00:00Z</VALID_FROM_DTS>
            <CREATED_DTS>2012-05-14T15:49:38.655+01:00</CREATED_DTS>
            <ITEMLIST>
                <ITEM>
                    <NAME>Adult Period Pass Corsham</NAME>
                    <DESC>1 week</DESC>
                    <SKU>ACT-38</SKU>
                    <PRICE>15</PRICE>
                    <CATEGORIES>
                        <CATEGORY>
                            <CategoryName>Product</CategoryName>
                            <CategoryDate>1 week</CategoryDate>
                            <CategorySortOrder>0</CategorySortOrder>
                        </CATEGORY>
                        <CATEGORY>
                            <CategoryName>Product Type</CategoryName>
                            <CategoryDate>Coach</CategoryDate>
                            <CategorySortOrder>0</CategorySortOrder>
                        </CATEGORY>
                        .....................

i would expecting something like

         <CATALOGUE>     <-----------missing!!!
            <COMMENT>LK</COMMENT>
            <VERSION>4</VERSION>
            <VALID_FROM_DTS>1990-01-25T00:00:00Z</VALID_FROM_DTS>
            <CREATED_DTS>2012-05-14T15:49:38.655+01:00</CREATED_DTS>
            <ITEMLIST>
                <ITEM>
                    <NAME>Adult Period Pass Corsham</NAME>
                    <DESC>1 week</DESC>
                    <SKU>ACT-38</SKU>
                    <PRICE>15</PRICE>
                    <CATEGORIES>
                        <CATEGORY>
                            <CategoryName>Product</CategoryName>
                            <CategoryDate>1 week</CategoryDate>
                            <CategorySortOrder>0</CategorySortOrder>
                        </CATEGORY>
                        <CATEGORY>
                            <CategoryName>Product Type</CategoryName>
                            <CategoryDate>Coach</CategoryDate>
                            <CategorySortOrder>0</CategorySortOrder>
                        </CATEGORY>
                        .....................
                 </CATALOGUE>

The Code used to Return the Object is

@Stateless
@Remote
@WebService
public class CatalogueManagerSoapService
{

    public CatalogueManagerSoapService()
    {
        // TODO Auto-generated constructor stub
    }

    @EJB
    private SOAPExportService userService;


    @WebMethod
    public Catalogue getLatestCatalogue(
            String username,
            String password,
            String catalogueName ) throws Exception
    { 
          Catalogue c = CatManager.getCatalogue();    
          return c;  
        }  

}

Answer my Own question

this is what the JAX-WS (JRS181) default implementation for @WebResult

@Retention(value=RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface WebResult {

    String name() default "return";
    String targetNamespace() default "";
    boolean header() default false;
    String partName() default "";
}

as you can see "return" is by default. change to @WebResult( name = "CATALOGUE") solved the problem

I think that you need a package-info.java file, try something like this:

@XmlSchema(namespace = "<your namespace>",

xmlns = @XmlNs(prefix = "<your prefix>", namespaceURI = "<your namespace>"),

elementFormDefault = XmlNsForm.QUALIFIED

)
@XmlAccessorType(XmlAccessType.NONE)
@XmlAccessorOrder(XmlAccessOrder.UNDEFINED)
package your.package.name;

import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

just put it in the root of your package, edit as needed and generate the xsd again. Should be ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM