cxf实现WebService
ws接口类
接口中的参数@WebParam(name = "userName") 当用soapUI调用时,输入参数会显示相应的userName名字,如果不加标签,显示为默认的arg0 arg1
1 package test.ws; 2 3 import javax.jws.WebParam; 4 5 /** 6 * WebService接口 实例 7 * @Description 8 * 类描述: 9 * @author gao 10 * @Date 2017年3月9日 11 * @modify 12 * 修改记录: 13 * 14 */ 15 public interface TestWs { 16 17 /** 18 * 测试webservice连接是否可用。 19 * @return Integer 20 */ 21 public String testWS(@WebParam(name = "userName") String userName); 22 23 }
ws接口实现类
实现类中参数@WebService(serviceName = "TestWebService")
package test.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.xml.bind.annotation.XmlTransient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * WebService实现类 实例 * @Description * 类描述: * @author gao * @Date 2017年3月9日 * @modify * 修改记录: * */ @WebService(serviceName = "TestWebService") public class TestWsImpl implements TestWs { protected Logger logger = LoggerFactory.getLogger(getClass()); /** * 测试webservice连接是否可用。(返回:输入值代表可用) * @return */ @WebMethod @Override @XmlTransient public String testWS(@WebParam(name = "userName") String userName) { return userName; } }
web.xml
<servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>test.action.StrutsWSFilter</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>test.*.action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
WebService过滤器 获取webservice请求的关键字“ws”
package test.action; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter; public class StrutsWSFilter extends StrutsPrepareAndExecuteFilter { @Override public void init(FilterConfig filterConfig) throws ServletException { super.init(filterConfig); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { String url = ((HttpServletRequest) req).getRequestURI(); if (url.indexOf("ws") < 0) { //另外一种过滤cxf方式 super.doFilter(req, res, chain); } else { chain.doFilter(req, res); } } }
applicationContext—webservice.xml
将WebService实现类配置到配置文件中,引入到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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="testWS" implementor="test.ws.TestWsImpl" address="/testWSaddress"> </jaxws:endpoint> </beans>
启动,soapUI测试正常