webService学习(三)webService发布到tomcat
1,创建web项目,导入jar包 2,因为要用CXF发布webService服务,所以在web.xml中配置CXF <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> 3,因为CXF?随tomcat启动的时候会自动去web-inf下加载一个叫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.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"> </beans> 4,因为 spring 配置文件是在启动的时候初始化,所以要在 WEB.XML中 添加: <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:bean.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 5,配置结束,发布一个基于Jax-Ws的WebService
//准备要发布的webService @WebService public class OneServiceImpl implements OneService{//有接口 List<String > list = new ArrayList<String>(); @Override public void add(String str) { list.add(str); System.out.println("OK") ; } @Override public List<String> getAll() { System.out.println("return all"); return list; } } 6,在bean.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.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"> <!-- oneService交给spring管理 --> <bean name="oneService" class="cn.zfx.interfaces.impl.OneServiceImpl"></bean> <!-- 和 Endpoint.publish("http://192.168.1.100:8888/one",new OneServiceImpl()); 有没有很类似? serviceClass是服务类的接口 --> <jaxws:server id="one" address="/one" serviceClass="cn.zfx.interfaces.OneService"> <jaxws:serviceBean> <bean class="cn.zfx.interfaces.impl.OneServiceImpl"></bean> </jaxws:serviceBean> </jaxws:server> </beans> 7,按常规的写法,写一个servlet,然后用url访问servlet,servlet访问oneServiceImpl
public class ServletOne extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { /* * WebApplicationContextUtils 是spring提供的工具类 * getServletContext()返回的是ServletContext(是一个容器,全局有效,spring初始化后,将WebApplicationContext以键值对方式放到了servletContext中) * WebApplicationContext 是spring的核心,应用的容器spring把bean放在这个容器中,需要的时候 getBean * */ WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); OneService oneService = context.getBean("oneService",OneService.class); oneService.add("jack"); } public void doPost(HttpServletRequest request,IOException { doGet(request,response); } 8,访问地址:http://localhost:8080/day02_zfx/ServletOne ?触发doGet方法,并且触发oneService的add方法 9,用webService直接调用oneService的方法, 有几种方式可以调用到,最简单的一种方式就是用Myeclipse或者Eclipse本身自带的工具 Lanuch SOAP Web Service Explore 工具测试 直接输入wsdl地址,然后测试 ? ?地址是:http://localhost:8080/day02_zfx/ws/ ? ? ?因为在web.xml中配置拦截的是/ws/*? Available SOAP services:
Available RESTful services: 10.,在发布一个Jax-Rs的WebService应用
@Path(value="/") //application/json 和 application/xml 谁先在前,优先接收谁 @Produces(value={"application/json","application/xml"}) public class TeacService { private List<String> list = new ArrayList<String>(); @GET @Path(value="/save/{name}") public void save(@PathParam(value="name")String nm){ System.err.println("name.........."+nm); list.add(nm); } @GET @Path(value="/all/") public ArrayOfList all(){ ArrayOfList ao = new ArrayOfList(); ao.setList(list); return ao; } } 11,在bean.xml中配置: <!-- jax-rs --> <bean id="teacService" class="cn.zfx.jaxrs.TeacService"></bean> <jaxrs :server id="teac" address="/teac"> <jaxrs:serviceBeans> <ref bean="teacService" /> </jaxrs:serviceBeans> </jaxrs:server> 然后再通过 ??http://localhost:8080/day02_zfx/ws/ ? 查看效果 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |