CXF + Spring 釋出 SOAP服務
阿新 • • 發佈:2018-11-08
web工程目錄如下:
實體類com.cc.entity.User如下:
package com.cc.entity; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; @XmlRootElement public class User implements Serializable{ private int id; private String name; private String telephone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
釋出SOAP服務com.cc.service.UserService:
package com.cc.service; import com.cc.entity.User; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * Created by CHENLIN on 2016/3/11. */ @WebService(name = "InvokeService", targetNamespace = "http://impl.service.cc.com") public interface UserService { @WebMethod(operationName = "getName", action = "urn:GetName") public User getName(@WebParam(name="userName") String userName); }
UserService介面實現com.cc.service.impl:
package com.cc.service.impl; import com.cc.entity.User; import com.cc.service.UserService; public class UserServiceImpl implements UserService{ @Override public User getName(String userName) { User user = new User(); user.setId(1); user.setName(userName); user.setTelephone("111"+userName); return user; } }
Spring的入口檔案web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- 配置Spring的配置檔案 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/bean.xml</param-value>
</context-param>
<!-- 配置Spring的web Context監聽器,將Spring與web工程整合在一起 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置CXF -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 使用CXF釋出服務 -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置需要暴露的SOAP服務的bean:
<?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">
<!--配置需要暴露的SOAP服務的bean-->
<jaxws:endpoint id="userServiceImplBean" address="/userSerivce"
implementor="com.cc.service.impl.UserServiceImpl"></jaxws:endpoint>
</beans>
啟動tomcat,檢視釋出的SOAP服務:
其中有幾點需要注意:服務釋出介面UserService中的命名是一一對應的:(註解@WebParam用於表示SOAP服務的引數名)