New to Jackson - XML response problem - Why have multiple nodes with duplicated element names

279 views
Skip to first unread message

o haya

unread,
Nov 29, 2021, 7:05:20 PM11/29/21
to jackson-user
Hi,

I have just started working with a Spring boot project.  This currently works and supports JSON responses, and I am trying to add support for XML responses.  Being new, it's been a struggle (that is an understatement :)!), but the last problem that I am encountering is that there are 2 situations where there are multiple levels with duplicated element names.  

The responses that I am working with are XACML 2.0 responses, and I think that they generated the model classes with XJC(?) and the XACML 2.0 XSD.  

The "expected" response message is (for example) supposed to look like:

<Response xmlns:ns2="urn:oasis:names:tc:xacml:2.0:policy:schema:os"
          xmlns:ns3="urn:oasis:names:tc:xacml:2.0:context:schema:os">
      <ns3:Result ResourceId="xxxxxxxxxxxxxxxxxxxx">
         <ns3:Decision>Permit</ns3:Decision>
         <ns3:Status>
            <ns3:StatusCode value="urn:oasis:names:tc:xacml:1.0:status:ok"/>
         </ns3:Status>
         <ns2:Obligations>
            <ns2:Obligation FulfillOn="Permit"
                            ObligationId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#Roles">
               
                  <ns2:AttributeAssignment AttributeId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#role"
                                           DataType="http://www.w3.org/2001/XMLSchema#string">AuthenticatedUser</ns2:AttributeAssignment>
               
            </ns2:Obligation>
            <ns2:Obligation FulfillOn="Permit"
                            ObligationId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#ResponseAttributes">
              
                  <ns2:AttributeAssignment AttributeId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#decisionTime"
                                           DataType="http://www.w3.org/2001/XMLSchema#dateTime">2021-11-28T18:27:09.800Z</ns2:AttributeAssignment>
               
            </ns2:Obligation>
         </ns2:Obligations>
      </ns3:Result>
</Response>

But, the responses that I actually getting look like the following:

<Response xmlns:ns2="urn:oasis:names:tc:xacml:2.0:policy:schema:os"
          xmlns:ns3="urn:oasis:names:tc:xacml:2.0:context:schema:os">
   <ns3:Result>
      <ns3:Result ResourceId=" xxxxxxxxxxxxxxxxxxxx  ">
         <ns3:Decision>Permit</ns3:Decision>
         <ns3:Status>
            <ns3:StatusCode value="urn:oasis:names:tc:xacml:1.0:status:ok"/>
         </ns3:Status>
         <ns2:Obligations>
            <ns2:Obligation FulfillOn="Permit"
                            ObligationId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#Roles">
               <ns2:AttributeAssignment>
                  <ns2:AttributeAssignment AttributeId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#role"
                                           DataType="http://www.w3.org/2001/XMLSchema#string">AuthenticatedUser</ns2:AttributeAssignment>
               </ns2:AttributeAssignment>
            </ns2:Obligation>
            <ns2:Obligation FulfillOn="Permit"
                            ObligationId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#ResponseAttributes">
               <ns2:AttributeAssignment>
                  <ns2:AttributeAssignment AttributeId="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#decisionTime"
                                           DataType="http://www.w3.org/2001/XMLSchema#dateTime">2021-11-28T18:27:09.800Z</ns2:AttributeAssignment>
               </ns2:AttributeAssignment>
            </ns2:Obligation>
         </ns2:Obligations>
      </ns3:Result>
   </ns3:Result>
</Response>

Notice that there are 2 levels with <ns3:Result> and also 2 levels with <ns2:AttributeAssignment>?

What kind of scenario might be causing that?

As I mentioned, I am really new to using Jackson, so please let me know what information might help to figure what the problem is?

Thanks,
Jim

gapag

unread,
Nov 30, 2021, 2:52:14 PM11/30/21
to jackson-user
Hi Jim,

¶ I guess you need to decorate your data classes with the XML specific @JacksonXmlElementWrapper to get the behaviour you wish.


¶ If you don't want/cannot decorate the class definition of the objects your are deserializing to, you can properly initialize the ObjectMapper:


BR,
-gapag
-----
On Tuesday, November 30, 2021 at 1:05:15 AM UTC+1 ohay...gmail.com wrote:
Hi,

I have just started working with a Spring boot project.  This currently works and supports JSON responses, and I am trying to add support for XML responses.  

Thanks,
Jim

o haya

unread,
Dec 1, 2021, 11:55:59 PM12/1/21
to jackson-user
Hi gapag,

I didn't show the code, but actually, I already had a @JacksonXmlElementWrapper, but I had the useWrapping set to the wrong value (misunderstood).  I changed it from true to false and that took care of the problem!!

Thanks!

Jim
Reply all
Reply to author
Forward
0 new messages