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, HttpServletResponse response)
			throws ServletException, 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:

OneService
  • add
  • getAll
Endpoint address: http://localhost:8080/day02_zfx/ws/one
WSDL : {http://interfaces.zfx.cn/}OneServiceService
Target namespace: http://interfaces.zfx.cn/


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/   查看效果











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值