使用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。
创建得到的包
运行得到结果: