0

I am building a spring boot application which accepts xml as input and produces xml as output. I can't use the spring boot default xml converter for parsing the input. I use jaxb library to parse them.

What I am doing is,

  1. I am generating java classes from xml schema(.xsd).
  2. Move the pojos as to spring boot application.
  3. Use the specific object as my input in my rest API's.
  4. Process the object and return the same object.

While returning the same object, I am not getting response as expected.

JAXBContext contextObj = JAXBContext.newInstance(Student.class);
Marshaller marshallerObj = contextObj.createMarshaller();

marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshallerObj.marshal(student, stringWriter);
return stringWriter.toString();

In order to get the expected result, I should have to change Response type as String and do the explicit marshalling. Is there any other way to rid of this?

Coming to the actual question, JAXB+Spring boot is the right way? or Steps which followed is correct?

  • You can use JAXB if you want; it works well in simple cases but it can become very cumbersome if the schema is large, complex, or frequently changing. My own preference for this kind of task would always be to use XSLT. – Michael Kay Jan 11 '21 at 08:31
  • Thanks @MichaelKay. Helps me a lot. – VIJAYKUMAR SUBRAMANI Jan 11 '21 at 10:20
  • @MichaelKay With using Spring boot, wondering I am explicitly marshalling the response. JAXBContext contextObj = JAXBContext.newInstance(Student.class); Marshaller marshallerObj = contextObj.createMarshaller(); marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshallerObj.marshal(student, stringWriter); return stringWriter.toString(); – VIJAYKUMAR SUBRAMANI Jan 11 '21 at 10:22

0 Answers0