Returning JAXB-generated elements from Spring Boot Controller

后端 未结 1 1634
春和景丽
春和景丽 2021-01-01 04:16

I\'m generating a plethora of Java files from http://www.ncpdp.org\'s XSD files (only available to members). After generating them, I\'d like to use them in my Spring Contro

相关标签:
1条回答
  • 2021-01-01 04:47

    I was able to solve this by creating a bindings.xjb file in the same directory as my schemas. This causes JAXB to generate @XmlRootElement on classes.

    <?xml version="1.0"?>
    <jxb:bindings version="1.0"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
                  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
    
        <jxb:bindings schemaLocation="transport.xsd" node="/xsd:schema">
            <jxb:globalBindings>
                <xjc:simple/>
            </jxb:globalBindings>
        </jxb:bindings>
    </jxb:bindings>
    

    To add namespaces prefixes to the returned XML, I had to modify the maven-jaxb2-plugin to add a couple arguments.

    <arg>-extension</arg>
    <arg>-Xnamespace-prefix</arg>
    

    And add a dependency:

    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-namespace-prefix</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
    

    Then modify my bindings.xjb to include this:

    <?xml version="1.0"?>
    <jxb:bindings version="1.0"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
                  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
                  http://jaxb2-commons.dev.java.net/namespace-prefix http://java.net/projects/jaxb2-commons/sources/svn/content/namespace-prefix/trunk/src/main/resources/prefix-namespace-schema.xsd">
    
        <jxb:bindings schemaLocation="transport.xsd" node="/xsd:schema">
            <jxb:globalBindings>
                <xjc:simple/>
            </jxb:globalBindings>
    
            <jxb:schemaBindings>
                <jxb:package name="org.ncpdp.schema.transport"/>
            </jxb:schemaBindings>
            <jxb:bindings>
                <namespace:prefix name="transport"/>
            </jxb:bindings>
        </jxb:bindings>
    </jxb:bindings>
    

    I learned how to do this from https://java.net/projects/jaxb2-commons/pages/Namespace-prefix. I also found http://blog.frankel.ch/customize-your-jaxb-bindings to be a good resource on how to customize JAXB bindings.

    0 讨论(0)
提交回复
热议问题