CXF(2.7.10) - Writing a service with Spring

时间:2023-11-16 12:45:50

1. 定义服务接口。

package com.huey.demo.ws;

import javax.jws.WebParam;
import javax.jws.WebService; @WebService
public interface HelloService { public String sayHello(@WebParam(name="who") String who); }

2. 实现服务接口。

package com.huey.demo.ws.impl;

import javax.jws.WebParam;
import javax.jws.WebService; import com.huey.demo.ws.HelloService; @WebService(name="helloService", endpointInterface="com.huey.demo.ws.HelloService")
public class HelloServiceImpl implements HelloService { public String sayHello(@WebParam(name="who") String who) {
String helloMsg = "Hello, " + who + "!";
return helloMsg;
} }

3. 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: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"> <jaxws:endpoint id="helloService"
implementor="com.huey.demo.ws.impl.HelloServiceImpl" address="/hello" /> </beans>

4. web.xml 配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>

5. 启动 Tomcat 运行 web 工程,在浏览器键入 http://localhost:8080/hellocxf/ws/hello?wsdl,验证服务是否发布成功(hellocxf 为工程名)。

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.ws.demo.huey.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://ws.demo.huey.com/" name="HelloServiceImplService" targetNamespace="http://impl.ws.demo.huey.com/">
<wsdl:import location="http://localhost:8080/hellocxf/ws/hello?wsdl=HelloService.wsdl" namespace="http://ws.demo.huey.com/"/>
<wsdl:binding name="HelloServiceImplServiceSoapBinding" type="ns1:HelloService">
<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="HelloServiceImplService">
<wsdl:port binding="tns:HelloServiceImplServiceSoapBinding" name="helloServicePort">
<soap:address location="http://localhost:8080/hellocxf/ws/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>