webservice(五) web项目发布webservice
1.web项目中发布webservice 步骤: 1.编写服务类 ? ?分为带接口的服务类和不带接口的服务类。当然推荐使用带接口的服务类。 2.在web.xml文件中配置所需的servlet: ? ?org.apache.cxf.transport.servlet.CXFServlet 3.提供一个spring配置文件:cxf-servlet.xml ? ?该文件的路径需要在web.xml中配置 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"> <display-name></display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置CXFServlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <!--以下代码可以放到 context-param中,则启动时就会加载。放在servlet中只会访问servlet时加载 --> <init-param> <!-- 配置spring的配置文件 --> <param-name>config-location</param-name> <param-value>/WEB-INF/cxf-servlet.xml</param-value> </init-param> </servlet> <!-- 配置所有的ws访问的路径 --> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 启动时加载用以优化 说明:以上配置会再首次访问servlet时才加载cxf-servlet.xml文件,效率较低。可以启动服务器时就加载cxf-servlet.xml文件。 做法:将servlet中的初始化参数init-param移到<context-param>中 <?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"> <display-name></display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 该配置是下面CXFServlet配置文件中init-param中移上来的,加快访问速度 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/cxf-servlet.xml</param-value> </context-param> <!-- 配置CXFServlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <!-- 配置所有的ws访问的路径 --> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 1.不带接口服务类 服务类: @WebService //@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) public class HelloService { public String sayHi(String name) { String s = "hello " + name; return s; } } cxf-servlet.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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- 引入CXF Bean定义如下,早期的版本中使用 --> <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" /> <!-- 第一种发布方式:简单发布(无接口的发布) --> <!-- id:唯一标识 implementor:服务实现类 address:服务的请求url--> <jaxws:endpoint id="helloService" implementor="cn.it.noInterface.HelloService" address="/hello"> <!-- 请求消息拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <!-- 响应消息拦截器 --> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:endpoint> </beans> 2.带接口的服务类 服务接口 @WebService //@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING) public interface IHelloService { public String sayHi(String name); } public class HelloServiceImpl implements IHelloService { public String sayHi(String name) { String s = "hello " + name; return s; } } cxf-servlet.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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <!-- 引入CXF Bean定义如下,早期的版本中使用 --> <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:server id="helloService2" serviceClass="cn.it.withInterface.IHelloService" address="/hello2"> <!-- 服务的实现类 --> <jaxws:serviceBean> <bean class="cn.it.withInterface.HelloServiceImpl"></bean> </jaxws:serviceBean> <!-- 请求消息拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <!-- 响应消息拦截器 --> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:server> </beans> 2.客户端的调用 客户端调用的几种方式:见博客?点击打开链接 这里使用通过spring配置文件的方式调用。 使用spring配置文件当然先要提供一个beans.xml文件。具体的调用代码如下: 配置文件:beans.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" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> <jaxws:client id="hello" address="http://localhost:8080/cxf_web/ws/hello2" serviceClass="remote.withInterface.IHelloService"></jaxws:client> </beans> 调用代码 public class MyTest { @Test public void test(){ ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); IHelloService helloService=(IHelloService)context.getBean("hello"); helloService.sayHi("kitty"); } } 关于web项目配置的说明 1.如果在项目中仅发布webservice,则可以只配置以下内容。 ? ?默认是读取WEB-INF/cxf-servlet.xml文件,所以是否指定都可以。 2.但一般情况下,一个web项目在使用了Spring之后,往往会通过ApplicationContext获取一些服务,此时则必须注册Spring的监听器。即以下代码: 3.在上页中出现的重复引入问题,可以修改cxf-servlet.xml文件。 4.在增加了Spring的监听器之后,即可以使用以下代码在Servlet中获取Spring的配置 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |