3

I'm developing a REST API that uses Spring MVC. The objects I consume and produce are generated (using JAXB) from NCPDP (http://www.ncpdp.org/) XSDs. I have everything working when requests come in, but I'd like to add additional attributes to the root element on outgoing requests.

Right now, my outgoing response looks as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<transport:Message xmlns:transport="http://www.ncpdp.org/schema/transport">
    <transport:Header>
        ...
    </transport:Header>
    <transport:Body>
        <transport:Status>
            <transport:Code>010</transport:Code>
            <transport:Description>OK</transport:Description>
        </transport:Status>
    </transport:Body>
</transport:Message>

And it should look as follows:

<?xml version="1.0"?>
<transport:Message xmlns:transport="http://www.ncpdp.org/schema/transport" xmlns:datatypes="http://www.ncpdp.org/schema/datatypes" 
    xmlns:script="http://www.ncpdp.org/schema/script" xmlns:structures="http://www.ncpdp.org/schema/structures" 
    xmlns:pa-structures="http://www.ncpdp.org/schema/pa-structures" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    StructuresVersion="v2014041" ECLVersion="v2014041" DatatypesVersion="v2014041" PA-StructuresVersion="v2014041" 
    TransactionVersion="v2014041" TransportVersion="v2014041" TransactionDomain="SCRIPT">
  <transport:Header>
    ...
  </transport:Header>
  <transport:Body>
    <transport:Status>
      <transport:Code>010</transport:Code>
    </transport:Status>
  </transport:Body>
</transport:Message>

I don't believe I need all the namespaces defined (since they're not used), but I do need the StructuresVersion and all other attributes. Is there a way to modify my bindings.xjb to include these attributes? Or do I have to copy the generated code into my source tree and add annotations to do this?

Thanks,

Matt

Matt Raible
  • 8,187
  • 9
  • 61
  • 120

1 Answers1

2

Since these attribute are declared in your XML schema, you should be getting appropriate properties in your schema-derived code.

It actually does not matter if this is a root element or some child element. Please re-check the generated code, look for something like getStructuresVersion(). So you don't need to do anything.

As for the namespaces, you will get them automatically declared, when you marshal. But you may first get them declared as ns0, ns1 etc. which is not very nice in terms of readability. Please see the following question:

Controlling namespace prefixes in JAXB

(the question itself, not the answers) for information on how to control these prefixes with a custom prefix mapper or this post by Blaise Doughan.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • 1
    Thanks! Didn't even think to look and see if I could call message.setStructuresVersion('v2014041'). I have the prefixes figured out thanks to a bindings.xjb - https://gist.github.com/mraible/abad8d78c1f053ec686b. – Matt Raible Oct 17 '14 at 19:15
  • @MattRaible This is good with `http://jaxb2-commons.dev.java.net/namespace-prefix` what you did there. Please consider writing a Q&A post on that, this will be valuable for other users as well. – lexicore Oct 17 '14 at 20:01