使用CXF处理的wsdl网页一般标签都用wsdl:开头。
如:
不使用CXF的,大都像下面这样规整,不是以wsdl开头的。
CXF+spring方式使用wsdl网页中的接口(这一切的前提是已经搭建好了CXF环境):
步骤一:在pom.xml中添加如下依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
步骤二:在web.xml中写如下代码
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value><!--指定文件路径-->
</context-param>
<listener>
<listener-class><!--监听器,用于加载spring配置文件-->
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>aa</servlet-name><!--名称随便取-->
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aa</servlet-name>
<url-pattern>/testcxf/*</url-pattern><!--请求地址包含testcxf这个的话才走CXFServlet,为了与ssm中的dispatcherservlet的/*区分开。-->
</servlet-mapping>
</web-app>
步骤三:在我们自定义的resources下的spring.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:p="http://www.springframework.org/schema/p"
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
">
<!--发布服务器端的接口-->
<!--提供服务的类,写的是接口的类--><!--根据具体情况往里面套即可, serviceClass是服务类,一般是一个接口-->
<jaxws:client id="abc"
serviceClass="cn.com.webxml.QqOnlineWebServiceSoap"
address="http://ws.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"></jaxws:client><!--serviceClass是我们创建对象的类的路径(包名加类名),address是访问wsdl网页的访问地址-->
</beans>
步骤四:将反向生成的类放在java文件的包下即可,包路径要与它本身(wsdl网页中那里的反应的包路径,注意是逆序读的)的路径一致
步骤五:在执行类中执行例如
main方法中
import cn.com.webxml.QqOnlineWebServiceSoap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
//ApplicationContext读取spring文件
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("spring.xml");
//创建spring中指定的路径的类的对象
QqOnlineWebServiceSoap abc = (QqOnlineWebServiceSoap)applicationContext.getBean("abc");
//调用QqOnlineWebServiceSoap对象的方法,查询QQ在线状态。
String s = abc.qqCheckOnline("1290623753");
System.out.println(s);
}
}
如果是写在Controller中(使用controller记得搭建好ssm框架,才能生效):
package controller;
import cn.com.webxml.QqOnlineWebServiceSoap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
@Controller
public class OnlinecOntroller {
@Resource
private QqOnlineWebServiceSoap qqOnlineWebServiceSoap;
@RequestMapping("/getState")
public String State(String QQ, ModelMap map){
//ApplicationContext读取spring文件
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("spring.xml");
//创建spring中指定的路径的类的对象
QqOnlineWebServiceSoap abc = (QqOnlineWebServiceSoap)applicationContext.getBean("abc");
//调用QqOnlineWebServiceSoap对象的方法,查询QQ在线状态。
String s = abc.qqCheckOnline(QQ);
map.addAttribute("QQState",s);
System.out.println("成功:"+s);
return "/index.jsp";
}
}
本次案例结构图: