2012-08-22 45 views
0

要在GlassFish應用服務器版3.1.1部署(建立12):有沒有的ObjectFactory與@XmlElementDecl

產生的原因:java.security.PrivilegedActionException: com.sun.xml.bind。 v2.runtime.IllegalAnnotationsException:1個計數爲 IllegalAnnotationExceptions對於元素 {http://www.w3.org/2004/08/xop/include}Include沒有ObjectFactory和 @XmlElementDecl。 此問題與以下位置有關: 位於protected java.util.List ru.gosuslugi.smev.rev120315.ReferenceType.content at ru.gosuslugi.smev.rev120315.ReferenceType at public javax.xml.bind.JAXBElement ru.gosuslugi.smev.rev120315.ObjectFactory.createReference(ru.gosuslugi.smev.rev120315.ReferenceType) at ru.gosuslugi.smev.rev120315.ObjectFactory at protected java.util.List ru.gosuslugi.smev.rev120315.AppDataType .ANY 在ru.gosuslugi.smev.rev120315.AppDataType 處ru.gosuslugi.smev.rev120315.MessageDataType 保護ru.gosuslugi.smev.rev120315.AppDataType ru.gosuslugi.smev.rev120315.MessageDataType.appData 在保護ru.go suslugi.smev.rev120315.MessageDataType ru.gosuslugi.smev.rev120315.BaseMessageType.messageData 在ru.gosuslugi.smev.rev120315.BaseMessageType

來源:

SRC /組織/ W3/_2004/_08/XOP /包含/ Include.java

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.08.22 at 04:13:30 PM MSK 
// 


package org.w3._2004._08.xop.include; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAnyAttribute; 
import javax.xml.bind.annotation.XmlAnyElement; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlSchemaType; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.namespace.QName; 


/** 
* <p>Java class for Include complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType name="Include"> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;any namespace='##other' maxOccurs="unbounded" minOccurs="0"/> 
*  &lt;/sequence> 
*  &lt;attribute name="href" use="required" type="{http://www.w3.org/2001/XMLSchema}anyURI" /> 
*  &lt;anyAttribute namespace='##other'/> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "Include", propOrder = { 
    "any" 
}) 
public class Include { 

    @XmlAnyElement(lax = true) 
    protected List<Object> any; 
    @XmlAttribute(name = "href", required = true) 
    @XmlSchemaType(name = "anyURI") 
    protected String href; 
    @XmlAnyAttribute 
    private Map<QName, String> otherAttributes = new HashMap<QName, String>(); 

    /** 
    * Gets the value of the any property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the any property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getAny().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link Object } 
    * 
    * 
    */ 
    public List<Object> getAny() { 
     if (any == null) { 
      any = new ArrayList<Object>(); 
     } 
     return this.any; 
    } 

    /** 
    * Gets the value of the href property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getHref() { 
     return href; 
    } 

    /** 
    * Sets the value of the href property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setHref(String value) { 
     this.href = value; 
    } 

    /** 
    * Gets a map that contains attributes that aren't bound to any typed property on this class. 
    * 
    * <p> 
    * the map is keyed by the name of the attribute and 
    * the value is the string value of the attribute. 
    * 
    * the map returned by this method is live, and you can add new attribute 
    * by updating the map directly. Because of this design, there's no setter. 
    * 
    * 
    * @return 
    *  always non-null 
    */ 
    public Map<QName, String> getOtherAttributes() { 
     return otherAttributes; 
    } 

} 

組織/ W3/_2004/_08/XOP /包含/ ObjectFactory.java

// 
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.08.22 at 04:13:30 PM MSK 
// 


package org.w3._2004._08.xop.include; 

import javax.xml.bind.JAXBElement; 
import javax.xml.bind.annotation.XmlElementDecl; 
import javax.xml.bind.annotation.XmlRegistry; 
import javax.xml.namespace.QName; 


/** 
* This object contains factory methods for each 
* Java content interface and Java element interface 
* generated in the org.w3._2004._08.xop.include package. 
* <p>An ObjectFactory allows you to programatically 
* construct new instances of the Java representation 
* for XML content. The Java representation of XML 
* content can consist of schema derived interfaces 
* and classes representing the binding of schema 
* type definitions, element declarations and model 
* groups. Factory methods for each of these are 
* provided in this class. 
* 
*/ 
@XmlRegistry 
public class ObjectFactory { 

    private final static QName _Include_QNAME = new QName("http://www.w3.org/2004/08/xop/include", "Include"); 

    /** 
    * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.w3._2004._08.xop.include 
    * 
    */ 
    public ObjectFactory() { 
    } 

    /** 
    * Create an instance of {@link Include } 
    * 
    */ 
    public Include createInclude() { 
     return new Include(); 
    } 

    /** 
    * Create an instance of {@link JAXBElement }{@code <}{@link Include }{@code >}} 
    * 
    */ 
    @XmlElementDecl(namespace = "http://www.w3.org/2004/08/xop/include", name = "Include") 
    public JAXBElement<Include> createInclude(Include value) { 
     return new JAXBElement<Include>(_Include_QNAME, Include.class, null, value); 
    } 

} 
+0

你是如何創建'JAXBContext'的? –

回答

1

從XML模式生成JAXB模型時,我始終建議使用生成的模型的包名稱來創建JAXBContext

JAXBContext jc = JAXBContext.newInstance("org.w3._2004._08.xop.include"); 

不指定要部署到GlassFish什麼樣的應用,但如果它是一個JAX-RS /澤西應用我會建議使用ContextResolver的。這是一個標準機制,可以讓您更好地控制JAXBContext的創建方式。下面是一個例子。

import java.util.*; 
import javax.ws.rs.Produces; 
import javax.ws.rs.ext.*; 
import javax.xml.bind.*; 
import org.eclipse.persistence.jaxb.JAXBContextFactory; 

@Provider 
@Produces({"application/xml", "application/json"}) 
public class SampleContextResolver implements ContextResolver<JAXBContext> { 

    private JAXBContext jc; 

    public SampleContextResolver() { 
     try { 
      jc = JAXBContext.newInstance("org.w3._2004._08.xop.include"); 
     } catch(JAXBException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public JAXBContext getContext(Class<?> clazz) { 
     if(clazz.getPackage().getName().equals("org.w3._2004._08.xop.include")) { 
      return jc; 
     } 
     return null; 
    } 

} 
+0

謝謝你的例子,但我做了SOAP服務。如何將JAXBContext添加到我的SOAP服務? –

+1

帶有jax-ws的SOAP。 –

+0

@AlexeySavchuk - 您是直接與SOAP服務交互,還是通過JAX-WS將JAXB用作默認綁定層? –

0

您可以嘗試將@XmlRoolElement註釋生成爲包含類。

+0

我將XmlRoolElement添加到Include.java.Does does not work –

+0

這似乎是您的SOAP編組器無法找到ObjectFactoy。當我們向編組指定錯誤的包時(例如Jaxb),我會發生這種情況。那麼,你如何使用這個Web服務?我的意思是通過Spring WebServiceTemplate或其他東西?什麼是marsheller/unmarshaller使用? – vikas

+0

它實際上應該是'@ XmlRootElement' – zygimantus

1

I 6個月前面臨完全一樣的問題,老實說,我不記得它是如何解決(也許我們並不需要一些功能,所以我們把它放在一邊)。但今天再次我們可愛的棧跟蹤與消息詛咒(字面上像你的)

但這次我能夠處理它。

Include類沒有問題。但不知何故,這是我的猜測,它不會爲jaxb正確加載,或者不在我們需要的時候加載,或者......無論如何。

我想在我的代碼中做的是驗證soap消息的有效載荷。

JAXBContext jaxbContext = JAXBContext.newInstance(UnifoTransferMsg.class) ; 
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
    Schema schema = schemaFactory.newSchema(new File("UnifoTransferMsg.xsd")); 

    Marshaller marshaller = jaxbContext.createMarshaller(); 
    marshaller.setSchema(schema); 
    marshaller.marshal(inputmsg, System.out); 

備用我的硬編碼值,它只是用於調試。儘管如此,第一行是拋出異常的地方,因爲Jaxb不知道如何包含或使用錯誤的類。爲了解決這個問題,需要在同一個調用中加載多個類。

所以對我來說,最終的修復是

JAXBContext.newInstance(Include.class, UnifoTransferMsg.class); 

希望有所幫助,也許有一天我會擴展更多的對這個問題的真正原因。