Spring / Jaxb2Marshaller / MOXy没有加载package-info.java名称空间映射

时间:2014-03-21 21:32:07

标签: java spring jaxb moxy

问题

我遇到了一些奇怪的行为:Spring Jaxb2Marshaller没有扫描我的package-info.java。有人可以看看吗?

我有一个Spring MVC / Camel应用程序,它接受用通用包装器包装的多个对象,其中XML必须如下所示:

<mlf:message id="22" product="great.event" xmlns:mlf="special mlf URI">
    <mlf:header>
        <mlf:action value="notify-published"/>
    </mlf:header>
    <mlf:payload status="partial">
        <can be any element at all>
    </mlf:payload>
</mlf:message>

我的设置遵循错误。

错误

-00:00 [localhost-startStop-1]错误o.s.web.context.ContextLoader上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建类路径资源[com / company / CamelConfig.class]中定义的名为'jaxb2Marshaller'的bean时出错:init方法的调用失败;嵌套异常是

...

异常[EclipseLink-25016](Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence.exceptions.XMLMarshalException 异常说明:在命名空间解析程序中找不到前缀mlf:header的命名空间。

  • 链接异常: [Exception [EclipseLink-0](Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5):org.eclipse.persistence.exceptions.IntegrityException

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)〜[spring-beans-4.0.1.RELEASE.jar:4.0.1.RELEASE]

设置

我的package-info.java和Wrapper类看起来像这样:

package-info.java

@XmlSchema(
    xmlns={
        @XmlNs(prefix="mlf", namespaceURI="special mlf URI")
    },
    elementFormDefault=XmlNsForm.UNQUALIFIED)

package com.company.camel;
import javax.xml.bind.annotation.*;

Message.java

package com.company.camel;
@XmlRootElement(name = "message", namespace = "special mlf URI")
@XmlAccessorType(XmlAccessType.FIELD)
public class Message {

    @XmlAttribute(required = false)
    private int id;

    @XmlAttribute(namespace = "")
    private String product;

    @XmlAttribute(namespace = "", required = false)
    private String type;

    @XmlPath("flm:header/flm:action/@value")
    private String action;

    @XmlPath("flm:header/flm:notify-failure/@type")
    private String notifyFailureType;

    @XmlPath("flm:header/flm:notify-failure/@value")
    private String notifyFailureValue;

    @XmlPath("flm:payload")
    private Payload payload;

    //SNIP
}

Payload.java

package com.company.camel;
@XmlRootElement(name="payload", namespace="special mlf URI")
@XmlAccessorType(XmlAccessType.FIELD)
public class Payload {

    @XmlAttribute
    private String status;

    @XmlAnyElement(lax=true)
    Object body;

    //Getters&setters snipped
}  

因为我需要从Spring MVC REST和Camel解组这个类(不,我不能使用Camel for REST),我在Spring中设置了JAXB:

@Configuration
@ComponentScan(basePackages="com.company.camel")
public class CamelConfig extends CamelConfiguration
{

    //SNIP

// The JAXBDataFormat is used inside Camel routes 
@Bean
public JaxbDataFormat jaxbDataFormat() {
    return new JaxbDataFormat(jaxb2Marshaller().getJaxbContext());
}

//used directly by Spring MVC
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setPackagesToScan(new String[]{"com.company"});
    return marshaller;
}

1 个答案:

答案 0 :(得分:0)

注意到你有另一个未得到回答的问题......我的哀悼,JAXB可能很难排除故障。你可以发布整个项目吗?

我们有一个大型的Spring项目,其中Spring从导入的JAR加载了带有package-info.java文件的JAXB注释类。

然后,另一位开发人员添加了代码,以便在导入的JAR中作为类中的类 在同一个包 中手动调用JAXBContext.newInstance。这个新类不在导入的JAR中,新类在主项目中。 此外,该软件包的主项目中没有package-info。

非常确定来自导入的JAR的原始JAXB包映射正在被吹走并被替换。

查找不应该对JAXB的任何其他调用。

相关问题