JAXB注释RootElement

时间:2012-05-29 23:30:39

标签: java xml java-ee jaxb

我有以下POJO,并且根元素没有显示在xml文件中(通过SOAP jax-ws调用发送),人们可以指出问题吗?

@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;

xml文件看起来像

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

用于返回对象的代码是

@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;  
        }  

}

2 个答案:

答案 0 :(得分:1)

回答我自己的问题

这是@WebResult

的JAX-WS(JRS181)默认实现
@Retention(value=RetentionPolicy.RUNTIME)
@Target({METHOD})
public @interface WebResult {

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

正如您所见,“返回”是默认情况。 更改为@WebResult(name =“CATALOG”)解决了问题

答案 1 :(得分:0)

我认为你需要一个package-info.java文件,尝试这样的事情:

@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;

将它放在包的根目录中,根据需要进行编辑并再次生成xsd。应该没问题。