gpt4 book ai didi

jax-ws - jax-ws/wsimport 中的 cdata

转载 作者:行者123 更新时间:2023-12-01 16:11:23 30 4
gpt4 key购买 nike

我使用 wsimport 创建了一个肥皂客户端,我需要将消息中字符串字段内的 xml 数据发送到网络服务器。我知道我实际上并不需要在 Web 服务调用中使用 cdata,但 Web 服务需要此字段位于 cdata 标记中。

问题是如何做到这一点。

为了从 wsdl 生成代码,我使用 jaxws-maven-plugin。在 Maven 配置中我使用绑定(bind)文件

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>

marshal/unmarschal 看起来像这样:

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();
}

这样我在数据字段中得到一个 ![CDATA[ 但 xml 是这样编码的

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

现在我在这里找到了这个(如何使用 jaxb 进行 cdata): http://odedpeer.blogspot.de/2010/07/jaxb-sun-and-how-to-marshal-cdata.html

但我不明白如何使用 Maven 插件和 wsimport 来做到这一点。我的意思是,我无法对其进行编码,它必须以任何方式进行配置。

你有什么想法如何做到这一点吗?

最佳答案

注释 @XmlCDATA ( read more ) 可以由 JAXB Annotate Plugin 自动生成。我使用的绑定(bind)如下。

<?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) 定义:

<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) 定义:

<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>

关于jax-ws - jax-ws/wsimport 中的 cdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9893531/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com