Cdata in jax-ws / wsimport

I created a soap client with wsimport, and I need to send the XML data inside the string field in the message to the web server. I know that I really don't need to use cdata in the webservice call, but the web service needs this field to be in the cdata tags.

The question is how to do this.

To generate code from wsdl, I use jaxws-maven-plugin. in maven configuration i use bind file

bindingFiles binding Filebinding.xjb /bindingFile /bindingFiles 
 jxb:bindings version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="urn:uniface:applic:services:BRF_IN" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"> <jxb:globalBindings generateElementProperty="false"/> <jxb:bindings scd="//element::tns:DATA"> <jxb:javaType name="String" parseMethod="de.xyz.CdataConverter.unmarshal" printMethod="de.xyz.CdataConverter.marshal" /> </jxb:bindings> 

and marshal / unmarschal looks like this:

 public class CdataConverter { private static final Pattern PATTERN = Pattern.compile("((?<=\\<\\!\\[CDATA\\[)[\\S\\s]+(?=\\]\\]\\>))"); private static final String CDATA_START = "<![CDATA["; private static final String CDATA_END = "]]>"; private final static Logger logger = Logger.getLogger(LgTestServer.class.getName()); public static String marshal(String input) { if (input == null) { return null; } PropertyConfigurator.configure(".\\log4j.properties"); logger.info("input --------------------->>>>>>>>\n" + input); return CDATA_START + input + CDATA_END; } public static String unmarshal(String cdatainput) { if (cdatainput == null) { return null; } Matcher matcher = PATTERN.matcher(cdatainput); if (matcher.find()) { return matcher.group(); } return cdatainput.trim(); } 

With this, I get a! [CDATA [in the data field, but xml is encoded as follows

 &lt;![CDATA[&lt;?xml version="1.0" encoding="UTF-8"?&gt;&#13; 

Now I found this here (how to make cdata with jaxb): http://odedpeer.blogspot.de/2010/07/jaxb-sun-and-how-to-marshal-cdata.html

but I don’t understand how to do this with the maven and wsimport plugin. I mean, I can’t encode it, it must be configured in some way.

Do you have any ideas how to do this?

+4
source share
1 answer

@XmlCDATA ( read more ) can be generated automatically by JAXB Annotate Plugin . The binding I use is below.

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" jaxb:version="2.1" xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="annox"> <!--Choose one--> <!--wsimport--><jaxb:bindings schemaLocation="service.wsdl#types?schema1" node="/xs:schema"> <!--wsdl2java--><jaxb:bindings schemaLocation="service.wsdl#types1" node="/xs:schema"> <jaxb:bindings node="//xs:complexType[@name='RegisterPaymentRequest']/xs:sequence/xs:element[@name='returnURL']"> <annox:annotate target="field"> <annox:annotate annox:class="org.eclipse.persistence.oxm.annotations.XmlCDATA"/> </annox:annotate> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings> 

jaxws-maven-plugin (wsimport) definition:

 <plugin> <groupId>org.jvnet.jax-ws-commons</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>2.2</version> <executions> <execution> <id>generate-sources-service</id> <phase>generate-sources</phase> <configuration> <sourceDestDir>${project.basedir}/src/main/java</sourceDestDir> <bindingDirectory>${project.basedir}/src/main/resources</bindingDirectory> <bindingFiles> <bindingFile>binding.xjb</bindingFile> </bindingFiles> <wsdlDirectory>${project.basedir}/src/main/resources</wsdlDirectory> <wsdlFiles> <wsdlFile>service.wsdl</wsdlFile> </wsdlFiles> <xjcArgs> <xjcArg>-Xannotate</xjcArg> </xjcArgs> </configuration> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics-annotate</artifactId> <version>0.6.4</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.4.0</version> </dependency> </dependencies> </plugin> 

cxf-codegen-plugin (wsdl2java) definition:

 <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>2.5.5</version> <executions> <execution> <id>generate-sources-service</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${project.basedir}/src/main/java</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl> ${project.basedir}/src/main/resources/service.wsdl </wsdl> <extraargs> <extraarg>-xjc-Xannotate</extraarg> <extraarg>-b</extraarg> <extraarg> ${project.basedir}/src/main/resources/binding.xjb </extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>annotate</artifactId> <version>0.4.1.5</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.4.0</version> </dependency> </dependencies> </plugin> 
+8
source

Source: https://habr.com/ru/post/1403706/


All Articles