微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐