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());
}
}
結果展示
(自用學習做個筆記,不喜勿噴)