Web Service整合spring

使用JAX-WS方式调用WebService

服务端:

spring容器配置文件:

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 				http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://cxf.apache.org/jaxws
      http://cxf.apache.org/schemas/jaxws.xsd
      http://cxf.apache.org/bindings/soap 
	http://cxf.apache.org/schemas/configuration/soap.xsd">
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
	<!-- 第一种配置方法 -->
	<!--  <bean id="wericeSerice" class="com.znsd.services.CityServiceimp"/>
	<jaxws:endpoint id="werictSerice" implementor="#wericeSerice" address="/services" publish="true"/>  -->

	<!-- 第二种配置方法 -->
	<jaxws:server serviceClass="com.znsd.services.CityServiceimp" 
	address="/services" />


web.xml 文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>CXF2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>
	org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
	org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

接口和实现类

@WebService
public interface CityService {
	public Map<String,ArrayList<Coit>> getProvince();
	public List<Coit> getCity(String province);

}
@WebService(endpointInterface = "com.znsd.services.CityService")
public class CityServiceimp implements CityService {
	private static Map<String,ArrayList<Coit>> provincies;
	static{
		provincies = new HashMap<>();
		ArrayList<Coit> c=new ArrayList<Coit>();
		c.add(new Coit(1,"深圳","好地方"));
		c.add(new Coit(1,"虎门","lj地方"));
		provincies.put("广州",c);
		ArrayList<Coit> c1=new ArrayList<Coit>();
		c1.add(new Coit(1,"永州","俺家"));
		c1.add(new Coit(1,"岳阳","lj地方"));
		provincies.put("湖南",c1);
	}

	@Override
	public Map<String, ArrayList<Coit>> getProvince() {
		return provincies;
	}

	@Override
	public List<Coit> getCity(String province) {
		return provincies.get(province);
	}

}

实体类

public class Coit {
	private int id;
	private String name;
	private String cott;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCott() {
		return cott;
	}
	public void setCott(String cott) {
		this.cott = cott;
	}
	public Coit(int id, String name, String cott) {
		super();
		this.id = id;
		this.name = name;
		this.cott = cott;
	}
	public Coit() {
		super();
	}
}

运行Web项目,然后部属到tomcat。

打开浏览器输入测试地址

在这里插入图片描述

在客户端的src目录使用wsimport命令编译webservice。

在这里插入图片描述

创建得到的包

在这里插入图片描述

运行得到结果:

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值