1、服务端
第一步:创建web项目,引入相关的jar包(Maven打war包)
(该用POM的)
第二步:创建接口,创建实现类,对象
package ws.cxf.server;
import javax.jws.WebService;
import ws.cxf.entity.User;
@WebService
public interface WeatherInterface {
public User queryWeather(User user);
}
package ws.cxf.server;
import ws.cxf.entity.User;
public class WeatherInterfaceImpl implements WeatherInterface {
public User queryWeather(User user) {
System.out.println("from client..."+user);
return new User(1L,"zhangsan",28);
}
}
public class User {
private Long id;
private String name;
private Integer age;
第三步:配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>CeShiServer</display-name>
<!-- spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- cxf的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
第四步:配置spring容器(applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- <jaxws:server>发布服务端的标签
address:/服务名称,ip和端口号由tomca提供
serviceClass:服务接口的全路径
serviceBean:服务实现类,需要让spring管理
-->
<jaxws:server address="/weather" serviceClass="ws.cxf.server.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 配置实现类的bean -->
<bean id="weatherInterface" class="ws.cxf.server.WeatherInterfaceImpl"/>
</beans>
第六步:将项目部署到tomcat,启动
第七步:测试
WSDL地址:http://localhost:8080/项目名/servlet的映射路径/服务名称?wsdl
http://localhost:8080/CeshiServer/ws/weather?wsdl
2、客户端
第一步:创建java项目,引入jar包
第二步:wsimport生成客户端代码(在客户端项目路径里,路径要和服务端路径一样)
第三步:配置spring容器(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 自动扫描该包 -->
<context:component-scan base-package="cn.itcast.ws.cxf.client" />
<!-- <jaxws:client>开发客户端
id:类似于bean标签的id属性,通过id属性可以获取接口实例
address:完整的服务地址,http://ip:端口号/项目名/servlet映射/服务名称,不是WSDL地址
serviceClass:服务接口(在生成代码中)
-->
<jaxws:client
id="weatherInterface"
address="http://localhost:8080/CeshiServer/ws/weather"
serviceClass="cn.itcast.ws.cxf.server.WeatherInterface">
</jaxws:client>
<mvc:annotation-driven />
<!-- 放行静态资源 -->
<mvc:default-servlet-handler />
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
第四步:调用查询方法
package cn.itcast.ws.cxf.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.itcast.ws.cxf.server.User;
import cn.itcast.ws.cxf.server.WeatherInterface;
public class WeatherClient {
public static void main(String[] args) {
//1.使用工厂类加载spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//2.从容器中获取接口实例
WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherInterface");
//3.调用方法
User user = new User();
user.setId(25L);
user.setAge(28);
user.setName("lili");
User user2 = weatherInterface.queryWeather(user);
System.out.println(user2.getName());
System.out.println(user2.getAge());
System.out.println(user2.getId());
}
}
结果展示
(自用学习做个笔记,不喜勿喷)