开发环境说明:
操作系统:Windows7_x64
JDK版本:jdk1.8.0_101
Eclipse版本:Mars.2 Release (4.5.2)
Web服务器:Tomcat7.X
Spring版本:spring-framework-4.2.5.RELEASE
CXF版本:CXF3.1.7
Eclispe工程项目:
1、新建一个Eclipse的WEB工程
引入了Spring4和CXF3的所有jar包,也可以选择必须的JAR包即可。
2、IWhat21Hello.java源码package com.what21.cxf3;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public interface IWhat21Hello {
/**
* WebService服务的方法
*/
@WebMethod
public String sayHi(@WebParam(name="text")String text);
}
3、What21HelloImpl.java源码package com.what21.cxf3;
import javax.jws.WebService;
@WebService(endpointInterface = "com.what21.cxf3.IWhat21Hello",
serviceName = "What21Hello")
public class What21HelloImpl implements IWhat21Hello {
@Override
public String sayHi(String text) {
System.out.println("请求的内容 -> " + text);
return text;
}
}
4、web.xml配置<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
0300_CXF3
contextConfigLocation
classpath:applicationContext-*.xml
org.springframework.web.context.ContextLoaderListener
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
1
CXFServlet
/webservice/*
index.jsp
注意这里的路径:url-pattern配置的内容和CXF配置的服务路径为WSDL的URL。
5、applicationContext-cxf.xml配置<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
implementor="#what21Hello" address="/what21Hello">
启动tomcat7后的WSDL访问地址:http://127.0.0.1:8080/CXF3/webservice/what21Hello?wsdl,使用SOAPUI导入WSDL地址测试后,服务调用成功。