1.根据wsdl文件生成相关代码
wsdl2java用法:
wsdl2java -p com -d src -all aa.wsdl
-p 生成代码的包名:
-d 指定要产生代码所在目录
-client 生成web service客户端代码
-server 生成web service服务器代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有代码:types,service proxy,,service interface, server mainline, client mainline,
2、maven依赖:
<properties>
<cxf.version>2.7.12</cxf.version>
</properties>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
3、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>qy-game-base</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
</web-app>
4、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<jaxws:endpoint id = "dataSyncService" address = "/dataSyncService"
implementorClass = "com.qy.crack.mm.action.DataSync" >
<jaxws:implementor >
<bean id = "dataSyncController"
class = "com.qy.crack.mm.action.DataSyncController" >
</bean >
</jaxws:implementor >
</jaxws:endpoint >
</beans>
遇到错误:
org.apache.cxf.interceptor.Fault: Unmarshalling Error: 意外的元素 (uri:"http://schema.ib.sdp.huawei.com", local:"ID")。所需元素为<{}ID>,<{}type>
原因:因为在客户端传过来的xml中对属性
<ns2:userID>
<ns3:ID xmlns:ns3="http://schasdlsds.asdsad.com">1527931163</ns3:ID>
<ns4:type xmlns:ns4="http://schasdlsds.asdsad.com">1</ns4:type>
</ns2:userID>
添加了命名空间,而我生成的代码中,命名空间是空的,所以报错,只要将@XmlElement(name = "ID", namespace = "", required = true)填入namespace=“http://schasdlsds.asdsad.com”即可
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "UserID", namespace = "http://www.csapi.org/schema/parlayx/data/v1_0", propOrder = {
"id",
"type"
})
public class UserID {
@XmlElement(name = "ID", namespace = "", required = true)
protected String id;
@XmlElement(namespace = "")
protected int type;
/**
*
* @return
* possible object is
* {@link String }
*
*/
public String getID() {
return id;
}
/**
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setID(String value) {
this.id = value;
}
/**
*
*/
public int getType() {
return type;
}
/**
*
*/
public void setType(int value) {
this.type = value;
}
}