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服务的参数名)