源码地址:https://github.com/sangbobo/webserviceTest
首先创建一个maven项目
pom.xml添加配置
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>apache-cxf</artifactId>
<version>3.2.4</version>
<type>pom</type>
</dependency>
截止2018年5月10日 apache-cxf 最新版本号是:3.2.4 最新版本号到https://mvnrepository.com/artifact/org.apache.cxf查询
ps:apache-cxf可以自行发布,也可以使用tomcat配合spring发布,首先介绍使用cxf发布
cxf发布代码编写
WebServiceTest业务逻辑
package pub.sangbo;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* Created by SangBigYe on 2018/5/10.
*/
@WebService
public class WebServiceTest {
public String sayHello(String name){
return "Hello " +name;
}
}
运行
package pub.sangbo;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
/**
* Created by SangBigYe on 2018/5/10.
*/
public class ServiceRun {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(WebServiceTest.class);
factory.setAddress("http://localhost:8080/service/hello");
Server server = factory.create();
server.start();
}
}
在浏览器中输入:http://localhost:8080/service/hello?wsdl 出现如下信息,代表发布成功了
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://sangbo.pub/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="WebServiceTestService" targetNamespace="http://sangbo.pub/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://sangbo.pub/" elementFormDefault="unqualified" targetNamespace="http://sangbo.pub/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
<wsdl:portType name="WebServiceTest">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WebServiceTestServiceSoapBinding" type="tns:WebServiceTest">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="WebServiceTestService">
<wsdl:port binding="tns:WebServiceTestServiceSoapBinding" name="WebServiceTestPort">
<soap:address location="http://localhost:8081/service/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端测试
package pub.sangbo;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
/**
* Created by SangBigYe on 2018/5/10.
*/
public class ClientRun {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/service/hello?wsdl");
Object[] objects;
try {
objects = client.invoke("sayHello", "SangBigYe");
//输出结果
System.out.println(objects[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出的结果
Hello SangBigYe
tomcat发布代码编写
代码编写
spring配置文件 applicationContext.xml
在java目录下创建conf包
在conf包下创建spring配置文件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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
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-servlet.xml" />
<jaxws:endpoint id="hello" address="/hello" implementor="pub.sangbo.WebServiceTest"/>
</beans>
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>cxfservice</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/conf/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行
使用tomcat启动,使用上面编写的 《客户端测试》 运行测试
运行结果
Hello SangBigYe