CXF XJC 的相关转换XML的问题
最近公司开发一个集中网管的web项目,其中用到webservice这项技术,在网上找了好多资料,不禁感叹还是好人多!此时发现自己从事这些年一直在网上索取,没有一点贡献,所以注册了简书,记录下自己这个过程,希望能帮助到一些人。
一、CXF用wsdl文件生成webservice的客户端和服务端
官网下载CXF ### https://cxf.apache.org/download.html
解压缩
配置环境变量
我这是win7,若是win10不知道怎么配置可自行百度
要配置一个JAVA_HOME的系统变量(不配在调用cxf的wsdl2java报错)
成功后在控制台输入 wsld2java -v 看到显示版本就成功了
通过wsdl文件生成客户端和服务端
注意:单独生成客户端或者服务端启动时会报错,缺少*ImplPortImpl.java这个文件,所以最好全部生成
启动客户端或者服务端
需要导入的依赖
<dependency>
<groupId>org.glassfish.main.javaee-api</groupId>
<artifactId>javax.jws</artifactId>
<version>3.1.2.2</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.3.0</version>
</dependency>
wsdl2java 命令说明
例如:wsdl2java -p com.zzzl.webservice.qidian -d d:\cxfoutput\src -all http://game.qidian.com/RemoteWebService/IPreventIndulge.asmx?wsdl
-p 也就是package 对应java中的包
-d 输入目录,生成.java文件会在该目录,会自动添加-p参数配置的包路径
-client 生成客户端测试web service的代码.
-server 生成服务器启动web service的代码.
-impl 生成web service的实现代码.
-encoding 指定生成的java文件字符编码,防止java文件内容中文乱码.
-ant 生成build.xml文件.
-all 生成上面-client -server -impl -ant 对应的所有文件
二、XJC用xsd文件生成Java对象
cd jdk所在的目录,打开命令行
xjc的使用方法:
例子:
xjc -p ns16 C:\Users\adm\Desktop\aaaa\ggg\DataDeny_Event_1.00.xsd -d C:\Users\adm\Desktop\aaaa\bean
指定字符编码为utf-8
xjc -encoding utf-8 -p xmlbean.ns20 C:\Users\adm\Desktop\complex\Event_1.00.xsd -d C:\Users\adm\Desktop\complex
执行xsd文件可能报错的例子:
解决办法:
-
1.对xsd文件的xs:schema声明添加绑定
image-20210802113610325.png -
2.修改已定义代码
原始代码:
image-20210802114040068.png
修改后:
image-20210802113917070.png
三、Java调用java对象生成对应的XML文件
配置依赖
<dependencies>
<dependency>
<groupId>org.glassfish.main.javaee-api</groupId>
<artifactId>javax.jws</artifactId>
<version>3.1.2.2</version>
</dependency>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
使用的xsd文件
<?xml version="1.0" encoding="gb2312"?>
<xs:schema xmlns="http://tempuri.org/ConfigPack" elementFormDefault="qualified" targetNamespace="http://tempuri.org/ConfigPack" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ConfigPack">
<xs:complexType>
<xs:sequence>
<xs:element ref="Config" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Config">
<xs:complexType>
<xs:attribute name="code" type="xs:string" use="required" />
<xs:attribute name="version" type="xs:string" use="required" />
<xs:attribute fixed="transient" name="mode" type="xs:string" />
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="description" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
生成Java的bean
- 调用xjc对ConfigPack.xsd生成Java文件,会生成四个Java文件
1). Config.java
2). ConfigPack.java
3). ObjectFactory.java
4). package-info.java - Config.java
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.08.02 时间 10:08:53 AM CST
//
package ns3;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>anonymous complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <attribute name="code" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="version" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="mode" type="{http://www.w3.org/2001/XMLSchema}string" fixed="transient" />
* <attribute name="id" type="{http://www.w3.org/2001/XMLSchema}string" />
* <attribute name="description" type="{http://www.w3.org/2001/XMLSchema}string" />
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "Config")
public class Config {
@XmlAttribute(name = "code", required = true)
protected String code;
@XmlAttribute(name = "version", required = true)
protected String version;
@XmlAttribute(name = "mode")
protected String mode;
@XmlAttribute(name = "id")
protected String id;
@XmlAttribute(name = "description")
protected String description;
/**
* 获取code属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getCode() {
return code;
}
/**
* 设置code属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setCode(String value) {
this.code = value;
}
/**
* 获取version属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getVersion() {
return version;
}
/**
* 设置version属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setVersion(String value) {
this.version = value;
}
/**
* 获取mode属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getMode() {
if (mode == null) {
return "transient";
} else {
return mode;
}
}
/**
* 设置mode属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setMode(String value) {
this.mode = value;
}
/**
* 获取id属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getId() {
return id;
}
/**
* 设置id属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setId(String value) {
this.id = value;
}
/**
* 获取description属性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getDescription() {
return description;
}
/**
* 设置description属性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setDescription(String value) {
this.description = value;
}
}
- ConfigPack.java
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.08.02 时间 10:08:53 AM CST
//
package ns3;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>anonymous complex type的 Java 类。
*
* <p>以下模式片段指定包含在此类中的预期内容。
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{http://tempuri.org/ConfigPack}Config"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"config"
})
@XmlRootElement(name = "ConfigPack")
public class ConfigPack {
@XmlElement(name = "Config", required = true)
protected Config config;
/**
* 获取config属性的值。
*
* @return
* possible object is
* {@link Config }
*
*/
public Config getConfig() {
return config;
}
/**
* 设置config属性的值。
*
* @param value
* allowed object is
* {@link Config }
*
*/
public void setConfig(Config value) {
this.config = value;
}
}
- ObjectFactory.java
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.08.02 时间 10:08:53 AM CST
//
package ns3;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the ns3 package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ns3
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link ConfigPack }
*
*/
public ConfigPack createConfigPack() {
return new ConfigPack();
}
/**
* Create an instance of {@link Config }
*
*/
public Config createConfig() {
return new Config();
}
}
- package-info.java
// 此文件是由 JavaTM Architecture for XML Binding (JAXB) 引用实现 v2.2.8-b130911.1802 生成的
// 请访问 <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// 在重新编译源模式时, 对此文件的所有修改都将丢失。
// 生成时间: 2021.08.02 时间 10:08:53 AM CST
//
@javax.xml.bind.annotation.XmlSchema(namespace = "http://tempuri.org/ConfigPack", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ns3;
通过Java生成xml
- ConfigTest.java
package com.demo;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import ns3.Config;
import ns3.ConfigPack;
import ns3.ObjectFactory;
public class ConfigTest {
public static void main(String[] args) throws JAXBException, IOException {
ObjectFactory factory = new ObjectFactory();
ConfigPack configPack = factory.createConfigPack();
configPack.setConfig(factory.createConfig());
Config con = configPack.getConfig();
con.setCode("SDMI_Config");
con.setVersion("1.00");
con.setMode("transient");
con.setId("123456");
con.setDescription("通信参数配置");
FileWriter writer = null;
JAXBContext context = JAXBContext.newInstance(ns3.ConfigPack.class);
try {
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshal.setProperty(Marshaller.JAXB_ENCODING, "gb2312");
// marshal.marshal(configPack, System.out);打印在控制台
//保存在内存中方便转发或者对其加密等操作
StringWriter writ = new StringWriter();
marshal.marshal(createViewPack, writ);
//为xml标签添加指定的命名空间(方便C语言的调用)
marshal.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return "ns3";
}
});
//写入文件中
writer = new FileWriter("ConfigPack.xml");
marshal.marshal(configPack, writer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 生成的ConfigPack.xml
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<ns3:ConfigPack xmlns="http://tempuri.org/ConfigPack">
<ns3:Config code="SDMI_Config" version="1.00" mode="transient" id="123456" description="通信参数配置"/>
</ns3:ConfigPack>
如果还是不清楚可以参考:https://www.w3cschool.cn/jaxb2/