2014-10-29 42 views
4

最近我一直在玩JAXB/MOXY,,它對我所有的測試和示例代碼都很有用。我專門使用綁定文件,這就是爲什麼我使用MOXY。JAXBException:「包」不包含ObjectFactory.class或jaxb.in​​dex

請注意,在我所有的例子中,我從來沒有使用ObjectFactory,也沒有使用jaxb.in​​dex,,它的工作原理是

當我回到自己的業務時,出現一個討厭的JAXB異常,說我的包不包含ObjectFactory或jaxb.in​​dex。

我的項目還調用Spring和Hibernate,JUnit和DBUnit。

以下是一些示例代碼:我有一個名爲AContributionPhysicalSupport的抽象類。

package org.pea.openVillages.pojo.contribution.implementation; 

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.DiscriminatorColumn; 
import javax.persistence.DiscriminatorType; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Inheritance; 
import javax.persistence.InheritanceType; 
import javax.persistence.Table; 

@Entity 
@Table(name = "TOV_CONTRIBUTION_PHYSICAL_SUPPORT") 
@Inheritance(strategy = InheritanceType.JOINED) 
@DiscriminatorColumn(name = "SUPPORT_TYPE", discriminatorType = DiscriminatorType.STRING, length = 20) 
public abstract class AContributionPhysicalSupport implements Serializable 
{ 
/* ***************************************************************** 
* 
* PROPERTIES 
* 
* ***************************************************************** 
*/ 

/** 
* for Serializable 
*/ 
private static final long serialVersionUID = 1L; 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
@Column(name = "SUPPORT_ID") 
private Long physicalSupportId; 

@Column(name = "SUPPORT_TYPE", nullable = false, length = 20, updatable = false, insertable = false) 
private String supportType; 

/* ***************************************************************** 
* 
* CONSTRUCTORS 
* 
* ***************************************************************** 
*/ 

public AContributionPhysicalSupport() 
{ 

} 

/* ***************************************************************** 
* 
* GETTERS AND SETTERS 
* 
* ***************************************************************** 
*/ 

/** 
* @return the physicalSupportId 
*/ 
public Long getPhysicalSupportId() 
{ 
    return physicalSupportId; 
} 

/** 
* @param physicalSupportId 
*   the physicalSupportId to set 
*/ 
public void setPhysicalSupportId(Long physicalSupportId) 
{ 
    this.physicalSupportId = physicalSupportId; 
} 

/** 
* @return the supportType 
*/ 
public String getSupportType() 
{ 
    return supportType; 
} 

/** 
* @param supportType 
*   the supportType to set 
*/ 
public void setSupportType(String supportType) 
{ 
    this.supportType = supportType; 
} 

/* ***************************************************************** 
* 
* UTILS 
* 
* ***************************************************************** 
*/ 

/* 
* (non-Javadoc) 
* 
* @see java.lang.Object#toString() 
*/ 
@Override 
public String toString() 
{ 
    return this.getClass() + " [physicalSupportId=" + physicalSupportId + ", supportType=" + supportType + "]"; 
} 

/* 
* (non-Javadoc) 
* 
* @see java.lang.Object#hashCode() 
*/ 
@Override 
public int hashCode() 
{ 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((physicalSupportId == null) ? 0 : physicalSupportId.hashCode()); 
    result = prime * result + ((supportType == null) ? 0 : supportType.hashCode()); 
    return result; 
} 

/* 
* (non-Javadoc) 
* 
* @see java.lang.Object#equals(java.lang.Object) 
*/ 
@Override 
public boolean equals(Object obj) 
{ 
    if (this == obj) 
    { 
     return true; 
    } 
    if (obj == null) 
    { 
     return false; 
    } 
    if (!(obj instanceof AContributionPhysicalSupport)) 
    { 
     return false; 
    } 
    AContributionPhysicalSupport other = (AContributionPhysicalSupport) obj; 
    if (physicalSupportId == null) 
    { 
     if (other.physicalSupportId != null) 
     { 
      return false; 
     } 
    } 
    else if (!physicalSupportId.equals(other.physicalSupportId)) 
    { 
     return false; 
    } 
    if (supportType == null) 
    { 
     if (other.supportType != null) 
     { 
      return false; 
     } 
    } 
    else if (!supportType.equals(other.supportType)) 
    { 
     return false; 
    } 
    return true; 
} 
} 

類視頻從AContributionPhysicalSupport繼承:

package org.pea.openVillages.pojo.contribution.implementation; 

import javax.persistence.Column; 
import javax.persistence.DiscriminatorValue; 
import javax.persistence.Entity; 
import javax.persistence.Table; 

@Entity 
@Table(name = "TOV_VIDEO") 
@DiscriminatorValue("VIDEO") 
public class Video extends AContributionPhysicalSupport 
{ 
/* ***************************************************************** 
* 
* PROPERTIES 
* 
* ***************************************************************** 
*/ 

/** 
* for serializable 
*/ 
private static final long serialVersionUID = 1L; 

@Column(name = "VIDEO_TYPE", nullable = false, length = 20) 
private String videoType; 

@Column(name = "VIDEO_SIZE") 
private Long videoSize; 

@Column(name = "VIDEO_LENGTH") 
private Long videoLength; 

/* ***************************************************************** 
* 
* CONSTRUCTORS 
* 
* ***************************************************************** 
*/ 

public Video() 
{ 
    super(); 
} 

/* ***************************************************************** 
* 
* GETTERS AND SETTERS 
* 
* ***************************************************************** 
*/ 

/** 
* @return the videoType 
*/ 
public String getVideoType() 
{ 
    return videoType; 
} 

/** 
* @param videoType 
*   the videoType to set 
*/ 
public void setVideoType(String videoType) 
{ 
    this.videoType = videoType; 
} 

/** 
* @return the videoSize 
*/ 
public Long getVideoSize() 
{ 
    return videoSize; 
} 

/** 
* @param videoSize 
*   the videoSize to set 
*/ 
public void setVideoSize(Long videoSize) 
{ 
    this.videoSize = videoSize; 
} 

/** 
* @return the videoLength 
*/ 
public Long getVideoLength() 
{ 
    return videoLength; 
} 

/** 
* @param videoLength 
*   the videoLength to set 
*/ 
public void setVideoLength(Long videoLength) 
{ 
    this.videoLength = videoLength; 
} 

/* ***************************************************************** 
* 
* UTILS 
* 
* ***************************************************************** 
*/ 

/* 
* (non-Javadoc) 
* 
* @see java.lang.Object#toString() 
*/ 
@Override 
public String toString() 
{ 
    return super.toString() + " Video [videoType=" + videoType + ", videoSize=" + videoSize + ", videoLength=" 
      + videoLength + "]"; 
} 

/* 
* (non-Javadoc) 
* 
* @see java.lang.Object#hashCode() 
*/ 
@Override 
public int hashCode() 
{ 
    final int prime = 31; 
    int result = super.hashCode(); 
    result = prime * result + ((videoLength == null) ? 0 : videoLength.hashCode()); 
    result = prime * result + ((videoSize == null) ? 0 : videoSize.hashCode()); 
    result = prime * result + ((videoType == null) ? 0 : videoType.hashCode()); 
    return result; 
} 

/* 
* (non-Javadoc) 
* 
* @see java.lang.Object#equals(java.lang.Object) 
*/ 
@Override 
public boolean equals(Object obj) 
{ 
    if (this == obj) 
    { 
     return true; 
    } 
    if (!super.equals(obj)) 
    { 
     return false; 
    } 
    if (!(obj instanceof Video)) 
    { 
     return false; 
    } 
    Video other = (Video) obj; 
    if (videoLength == null) 
    { 
     if (other.videoLength != null) 
     { 
      return false; 
     } 
    } 
    else if (!videoLength.equals(other.videoLength)) 
    { 
     return false; 
    } 
    if (videoSize == null) 
    { 
     if (other.videoSize != null) 
     { 
      return false; 
     } 
    } 
    else if (!videoSize.equals(other.videoSize)) 
    { 
     return false; 
    } 
    if (videoType == null) 
    { 
     if (other.videoType != null) 
     { 
      return false; 
     } 
    } 
    else if (!videoType.equals(other.videoType)) 
    { 
     return false; 
    } 
    return true; 
} 

} 

這裏是我的綁定文件(還有其他類的繼承AContributionPhysicalSupport ....)

<?xml version="1.0" encoding="UTF-8"?> 
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="org.pea.openVillages.pojo.contribution.implementation"> 

<java-types> 

    <java-type name="AContributionPhysicalSupport"> 
     <xml-root-element name="contribution-physical-support" /> 
     <xml-type prop-order="physicalSupportId supportType" /> 
     <java-attributes> 
      <xml-attribute java-attribute="physicalSupportId" name="support-id" /> 
      <xml-element java-attribute="supportType" name="support-type" /> 
     </java-attributes> 
    </java-type> 

    <java-type name="ExternalFileFormat"> 
     <xml-root-element name="ext-file-format" /> 
     <xml-type prop-order="fileType fileSize" /> 
     <java-attributes> 
      <xml-element java-attribute="fileType" name="file-type" /> 
      <xml-element java-attribute="fileSize" name="file-size" /> 
     </java-attributes> 
    </java-type> 

    <java-type name="InternalFileFormat"> 
     <xml-root-element name="int-file-format" /> 
     <xml-type prop-order="fileSize" /> 
     <java-attributes> 
      <xml-element java-attribute="fileSize" name="file-size" /> 
     </java-attributes> 
    </java-type> 

    <java-type name="Video"> 
     <xml-root-element name="video" /> 
     <xml-type prop-order="videoType videoSize videoLength" /> 
     <java-attributes> 
      <xml-element java-attribute="videoType" name="video-type" /> 
      <xml-element java-attribute="videoSize" name="video-size" /> 
      <xml-element java-attribute="videoLength" name="video-length" /> 
     </java-attributes> 
    </java-type> 

</java-types> 

</xml-bindings> 

現在我的測試:

@Test 
public void testYATMarshal() throws Exception 
{ 
    Video toto = new Video(); 
    toto.setPhysicalSupportId(new Long(1)); 
    toto.setSupportType("VIDEO"); 
    toto.setVideoLength(new Long(358)); 
    toto.setVideoSize(new Long(5775)); 
    toto.setVideoType("avi"); 

    FileReader videoBindFile = new FileReader(
      "src/main/resources/xml-mapping/ContributionImpl-binding.xml"); 
    List<Object> videoBindList = new ArrayList<Object>(); 
    videoBindList.add(videoBindFile); 

    Map<String, List<Object>> videoMetaMap = new HashMap<String, List<Object>>(); 
    videoMetaMap.put("org.pea.openVillages.pojo.contribution.implementation", videoBindList); 
    Map<String, Object> videoProperties = new HashMap<String, Object>(); 
    videoProperties.put(JAXBContextProperties.OXM_METADATA_SOURCE, videoMetaMap); 

    JAXBContext context = JAXBContext.newInstance("org.pea.openVillages.pojo.contribution.implementation", 
      Video.class.getClassLoader(), videoProperties); 
    Marshaller marshaller = context.createMarshaller(); 
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

    marshaller.marshal(toto, System.out); 
} 

和最後但並非最不重要的例外:

javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "org.pea.openVillages.pojo.contribution.implementation" doesnt contain ObjectFactory.class or jaxb.index 
- with linked exception: 
[javax.xml.bind.JAXBException: "org.pea.openVillages.pojo.contribution.implementation" doesnt contain ObjectFactory.class or jaxb.index] 
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:146) 
at javax.xml.bind.ContextFinder.find(ContextFinder.java:347) 
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431) 
at org.pea.openVillages.dao.service.impl.ContributionDAOImplTest.testYATMarshal(ContributionDAOImplTest.java:187) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: javax.xml.bind.JAXBException: "org.pea.openVillages.pojo.contribution.implementation" doesnt contain ObjectFactory.class or jaxb.index 
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:216) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:172) 
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132) 
... 33 more 

更多信息:

  • 我的測試包(在那裏我有我的JUnit測試類)包含jaxb.properties文件
  • 我的JUnit測試class實際上是一個DBUnit/SpringJUnit類,因爲我想編組來自DB的對象
  • 從DB進行編組或者編組簡單對象(如我的示例中所示)會生成相同的異常
  • 當然,在包中添加jaxb.in​​dex並不會改變任何東西

四隻眼睛比兩隻好。我不明白我做錯了什麼。如果有人看到它,請讓我知道。

乾杯

回答

4

Provider com.sun.xml.internal.bind.v2.ContextFactory could not be instantiated - 好像你不使用莫西但內置的JAXB RI。

請布萊斯檢查下面的帖子:

Specifying EclipseLink MOXy as Your JAXB Provider

參見以下問題:

Does MOXy need anything special when using with schema-derived classes?

+0

Thx for answering。 我剛剛發現它與我編組對象的包有關。 讓我解釋一下: 我有2個JUnit測試。第一個是在包裝 org.pea.openVillages.dao.service.impl 和其他在 org.pea.openVillages.pojo.contribution.implementation (相同的包名稱作爲我的班,但在src/test/java) 從第一個測試包進行編組**不起作用**,而從第二個**開始的編組工作** ** 爲什麼我需要知道的是:我在哪裏放置jaxb.properties文件能夠從第一個包裝中收集嗎? – avi613 2014-10-30 22:25:58

+0

只是爲了讓自己清楚:我通過在某處放置正確的jaxb.properties來使用MOXy。我需要知道的是我該如何從其他地方執政?我可以做嗎 ? – avi613 2014-10-30 22:29:01

+1

@ avi613嘗試用'META-INF/services'作爲Blaise描述的方法:http://stackoverflow.com/questions/26411849/does-moxy-need-anything-special-when-using-with-schema-derived - 類 – lexicore 2014-10-31 12:58:04

1

答案由Lex提供icore在他的評論中爲我做了!

我已經設置好的兩包,如圖Blaise's answer

JAXBContext jc = JAXBContext.newInstance("com.example.pkg1:org.example.pkg2"); 

這做到了!

Thx man

相關問題