0

I have an XSD Schema with the following element:

<xs:element name="FooResponse" type="commonTypes:Response" />

When I run the build (which has a xjc goal for converting the schema to Java sources), a separate class file FooResponse.java is not created (probably because it would be identical to Response.java. Now, I know I can rewrite the schema like this:

<xs:element name="FooResponse">
        <xs:complexType>
            <xs:complexContent>
                <xs:extension base="commonTypes:Request"/>
            </xs:complexContent>
        </xs:complexType>
    </xs:element>

and get the result I'm looking for. However, the schema is automatically generated from a tool, and I'd rather not have to touch it manually. Is there a way to configure the plugin to create the source file in this situation?

I need to get both Response.java and FooResponse.java created, as there are other types unrelated to FooResponse that need to inherit from that.

I tried creating an XJB binding file like this:

<jaxb:bindings version="2.0"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jaxb:bindings schemaLocation="my_schema.xsd" node="/xs:schema/xs:element[@name='FooResponse']">
            <jaxb:class name="FooResponse"/>
    </jaxb:bindings>

</jaxb:bindings>

This works in that I get both Response.java and FooResponse.java but the contents of FooResponse is not exactly what I'd like:

public class FooResponse
    extends JAXBElement<Response>
{

    protected final static QName NAME = new QName("initech.com/c/m", "FooResponse");

    public FooResponse(Response value) {
        super(NAME, ((Class) Response.class), null, value);
    }

    public FooResponse() {
        super(NAME, ((Class) Response.class), null, null);
    }

}

This would be exactly what I want if I got it to extend Response directly without wrapping it into a JAXBElement.

  • 1
    Possible duplicate of [JAXB: How to change XJC-generated classes names when attr type is specified in XSD?](https://stackoverflow.com/questions/4793637/jaxb-how-to-change-xjc-generated-classes-names-when-attr-type-is-specified-in-x) You should be able to adjust the `node` xpath in the answer to your element. – daniu May 24 '18 at 13:38
  • Not exact duplicate, though it may have prodded me in the correct direction. That question is about renaming (in my case) `Response` to `FooResponse`. I need to keep both `Response` and `FooResponse`, because there are other types that are derived from `Response` that should not change their parent type to `FooResponse`. – Teemu Mäki May 25 '18 at 05:29
  • 1
    This question is actually closer to my problem: https://stackoverflow.com/questions/43399861/jaxb-xjc-generate-classes-from-elements-with-same-complextype – Teemu Mäki May 25 '18 at 08:58
  • [tag:maven-jaxb2-plugin] does not have an `xjc` goal. – lexicore May 25 '18 at 21:19
  • Why do you actually need this? – lexicore May 25 '18 at 21:20
  • This is `jaxb2-maven-plugin` from `org.codehaus.mojo` which does have an xjc goal. Without going too much into details, I need this for a work related thing where XML payloads defined in schemas are passed around as messages. – Teemu Mäki May 28 '18 at 04:19
  • Currently it kinda does what I need, if the type is put into a different package than where it is referenced, the plugin creates a factory method for creating the response, but that one has the `JAXBElement` wrapper too, which may be troublesome when marshalling messages to/from the message queue. – Teemu Mäki May 28 '18 at 04:24

0 Answers0