plugin jaxb2marshaller generate from java spring spring-boot marshalling jaxb2-maven-plugin

java - generate - Agregue CDATA usando org.springframework.oxm jaxb2marshaller



maven jaxb2 plugin generate from local wsdl (1)

Encontré la solución con integración moxy (no pude encontrar otra alternativa), publicando aquí si ayuda a alguien que lo necesita.

importado dependencia moxy y archivo jaxb.properties agregado en el mismo paquete donde se crea bean con la siguiente línea:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

y poner la anotación @XmlCDATA en el campo requerido. Esto generó el archivo xml con secciones CDATA.

Estoy teniendo un gran problema mientras clasifico pocos elementos en XML con CDATA usando jaxb2marshaller. He analizado las soluciones como:

JAXB Marshalling Unmarshalling con CDATA

¿Cómo generar bloque CDATA usando JAXB?

y mucho más, pero no pudo encontrar una solución adecuada. O le dicen que cambie a la implementación anterior de JAXB o que use MOXY. Pero, este no es mi requisito. He implementado debajo de dos clases utilizando la biblioteca OXM y quiero generar un XML donde pocos elementos necesitan tener un CDATA adjunto.

import java.util.HashMap; import java.util.Map; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.jaxb.Jaxb2Marshaller; @Configuration public class AppConfig { @Bean public Processor getHandler(){ Processor handler= new Processor(); handler.setMarshaller(getCastorMarshaller()); handler.setUnmarshaller(getCastorMarshaller()); return handler; } @Bean public Jaxb2Marshaller getCastorMarshaller() { Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); jaxb2Marshaller.setPackagesToScan("com.pom.dom.whatever.model"); Map<String,Object> map = new HashMap<String,Object>(); map.put("jaxb.formatted.output", true); jaxb2Marshaller.setMarshallerProperties(map); return jaxb2Marshaller; } }

y

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.springframework.oxm.Marshaller; import org.springframework.oxm.Unmarshaller; public class Processor { private Marshaller marshaller; private Unmarshaller unmarshalling; public void setMarshaller(Marshaller marshaller) { this.marshaller = marshaller; } public void setUnmarshaller(Unmarshaller unmarshalling) { this.unmarshaller = unmarshaller; } //Converts Object to XML file public void objectToXML(String fileName, Object graph) throws IOException { FileOutputStream fos = null; try { fos = new FileOutputStream(fileName); marshaller.marshal(graph, new StreamResult(fos)); } finally { fos.close(); } } //Converts XML to Java Object public Object xmlToObject(String fileName) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(fileName); return unmarshaller.unmarshal(new StreamSource(fis)); } finally { fis.close(); } } }

En la clase principal:

generateXML(){ public void generateCheckXML(ReportDTO repDTO, String fileName){ AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); Processor processor = ctx.getBean(Processor.class); ObjectFactory objectFactory = new ObjectFactory(); TRIMSInterface trimsInterface = objectFactory.createTRIMSInterface(); // setters processor.objectToXML(fileName,trimsInterface); } }

y una clase POJO simple con setters y getters para producir el XML.

¿Puedo hacer algunos cambios arriba para producir el XML con el atributo CDATA requerido?

NOTA : Ya probé EclipseLink Moxy (@XmlData) y no se integra con OXM. Estoy buscando implementar esto sin usar un jar de terceros en mi código.