1

我正在尝试marshall/unmarshall一个没有@xmlrootelement. 任何人都可以帮忙吗?我正在使用的编组器是 Spring 的 Jaxb2Marshaller 类型的org.springframework.oxm.jaxb 大多数解决方案都是类型javax.xml.bind MarshallerJAXBContext. marshalling在没有任何@xmlrootelement提供的 Java 类中需要帮助,使用 Spring 的JAXB2MARSHALLER.

我无法添加,@xmlrootelement因为这是外部.jar

我已经尝试过所有这些解决方案,它们实际上提供了一种使用 javax 而不是 spring 进行编组/解组的方法

无法将类型编组为元素,因为它缺少自动生成的类的 @XmlRootElement 注释

无法将类型编组为 XML 元素,因为缺少 @XmlRootElement 注释

包括这个有一个非常简洁的解释但使用 Marshaller 而不是 JaxB2Marshaller

https://howtodoinjava.com/jaxb/marshal-without-xmlrootelement/

类需要经过 Marshalling 没有@xmlrootelement

PartnerRequest.java 类具有@xmlrootelement

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "",
    propOrder = {"partnerCdId", "clientProductCode"}
)
@XmlRootElement(
    name = "PartnerRequest"
)
public class PartnerRequest



    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(
        name = "Partner",
       )
    public class Partner{
    }

我得到的异常如下:

[com.sun.istack.internal.SAXException2: unable to marshal type "com.svc.partner.v1.PartnerRequest" as an element because it is missing an @XmlRootElement annotation]
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.convertJaxbException(Jaxb2Marshaller.java:933)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:709)
    at org.springframework.ws.support.MarshallingUtils.marshal(MarshallingUtils.java:81)
    at org.springframework.ws.client.core.WebServiceTemplate$2.doWithMessage(WebServiceTemplate.java:395)
    at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:573)
    at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:539)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:386)
    at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380)
    at com.vmd.services.BaseService.marshallSendAndReceive(BaseService.java:53)
    at com.vmd.services.PartnerServiceImpl.getPartner(PartnerServiceImpl.java:27)
    at com.vmd.services.PartnerServiceImplTest.ShouldGetPartner(PartnerServiceImplTest.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: javax.xml.bind.MarshalException: null
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.marshal(Jaxb2Marshaller.java:705)
    ... 39 common frames omitted
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.svc.partner.v1.PartnerRequest" as an element because it is missing an @XmlRootElement annotation
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:234)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:323)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:479)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 41 common frames omitted

另外,请记住,我们需要使用 Jaxb2Mashaller 而不是 Marshaller 对其进行编组。这是一个外部 jar/依赖项不可编辑的代码。

提前致谢。

4

4 回答 4

0

我使用 JAxBElement 修复了类似的异常:

JAXBContext jaxbContext = JAXBContext.newInstance(Foo.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

请尝试一次,如果它不起作用,请分享一个示例 XSD 以供进一步调查。

于 2019-01-31T17:51:40.613 回答
0

我终于能够解决这个问题,问题出在包名称上,我似乎导入了错误的包,它抱怨partnerRequest缺少@xmlrootelement.

因此,最后,当我们有两个同名的类以及使用正确的包导入时,我们需要小心。感谢大家!

于 2019-02-04T15:00:32.923 回答
0

在 Spring Boot 2 中,我通常使用

@Bean
MessageConverter getMessageConveter() {
    Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setContextPath(ObjectFactory.class.getPackage().getName());
    Map<String, Object> prop = new HashMap<>();
    prop.put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    jaxb2Marshaller.setMarshallerProperties(prop);
    jaxb2Marshaller.setCheckForXmlRootElement(false);
    return new MarshallingMessageConverter(jaxb2Marshaller);
}

但是仍然收到错误。直到我发现我试图编组复杂类型。一旦我把这个对象包装在一个

<xs:element name="..."

它按预期生成了一个@XMLRootElement注释。

于 2019-07-08T17:56:55.480 回答
0

如果没有解析器知道根元素,就无法进行编组和解组。因此,当您@XmlRootElement在数据类中没有时,您可以绕过 using JAXBElement,在其中提供包装在其中的模型类,以便编组器在解析时知道要引用的源类结构。

我在下面包含了使用JAXBElementwith spring的工作示例Jaxb2Marshaller

@SpringBootApplication
public class XmlTransformationNoRoot {

    public static void main(String[] args) {
        SpringApplication.run(XmlTransformationNoRoot.class, args);
    }

}

@Component
class TestTransformation implements CommandLineRunner {

    public void run(String[] args) throws JAXBException {

        //marshalling
        String testXML = marshallXml(new Partner("HelloWorld"));
        System.out.println("Marshalled : " + testXML);

        //unmarshalling
        System.out.println("Unmarshalled : " + unmarshallXml(testXML, Partner.class));
    }

    public <T> String marshallXml(T object) throws JAXBException {
        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        jaxb2Marshaller().marshal(new JAXBElement(
                new QName(object.getClass().getPackage().getName(), object.getClass().getSimpleName()),
                object.getClass(), object), result);
        return sw.toString();
    }

    @SuppressWarnings("unchecked")
    public <T> T unmarshallXml(String source, Class<T> clazz) throws JAXBException {
        Jaxb2Marshaller marshaller = jaxb2Marshaller();
        //this line voids singleton use of Jaxb2Marshaller
        marshaller.setMappedClass(clazz);
        return (T) marshaller.unmarshal(new StreamSource(new StringReader(source)));
    }

    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setMarshallerProperties(new HashMap() {{
            put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
        }});
        marshaller.setClassesToBeBound(new Class[]{Partner.class});
        return marshaller;
    }
}

希望这可以帮助。可执行源代码可在我的github 存储库中找到。

于 2019-01-31T20:08:20.793 回答