WebService 的CXF框架 WS方式Spring开发

时间:2023-03-09 01:53:58
WebService 的CXF框架  WS方式Spring开发

1.建项目,导包.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn_itcast.maven</groupId>
<artifactId>cxf_ws_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cxf_ws_spring</name>
<description>CXF的WS整合Spring发布</description> <dependencies>
<!-- CXF WS开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- Spring开发 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- Spring整合junit开发 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9998</port>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.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"
id="WebApp_ID" version="2.5"> <!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF基于web访问 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 加载级别 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- 欢迎页面 -->
<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>

web.xml

2.导入实体类/service

 package cn.itcast.cxf.service;

 import java.util.List;

 import javax.jws.WebMethod;
import javax.jws.WebService; import cn.itcast.cxf.domain.Car;
import cn.itcast.cxf.domain.User; @WebService
public interface IUserService {
@WebMethod
public String sayHello(String name); @WebMethod
public List<Car> findCarsByUser(User user);
}

IUIservice

 package cn.itcast.cxf.service;

 import java.util.ArrayList;
import java.util.List; import javax.jws.WebService; import cn.itcast.cxf.domain.Car;
import cn.itcast.cxf.domain.User;
//设置endpointInterface接口服务完整类名,serviceName服务名称.
@WebService(endpointInterface = "cn.itcast.cxf.service.IUserService", serviceName = "userService")
public class UserServiceImpl implements IUserService { // 简单参数传递
public String sayHello(String name) {
return "Hello," + name;
}
// 复杂参数传递
public List<Car> findCarsByUser(User user) {
if ("xiaoming".equals(user.getUsername())) {
List<Car> cars = new ArrayList<Car>();
Car car1 = new Car();
car1.setId(1);
car1.setCarName("大众途观");
car1.setPrice(200000d);
cars.add(car1); Car car2 = new Car();
car2.setId(2);
car2.setCarName("现代ix35");
car2.setPrice(170000d);
cars.add(car2); return cars;
} else {
return null;
}
}
}

UserviceImpl

 package cn.itcast.cxf.domain;

 public class Car {
private Integer id;
private String carName;
private Double price; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getCarName() {
return carName;
} public void setCarName(String carName) {
this.carName = carName;
} public Double getPrice() {
return price;
} public void setPrice(Double price) {
this.price = price;
} @Override
public String toString() {
return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";
} }

Car

 package cn.itcast.cxf.domain;

 import java.util.ArrayList;
import java.util.List; public class User {
private Integer id;
private String username;
private String city; private List<Car> cars = new ArrayList<Car>(); public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public List<Car> getCars() {
return cars;
} public void setCars(List<Car> cars) {
this.cars = cars;
} }

User

3.配置Springcxf服务发布

 <?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--
address 客户端访问服务路径
serviceClass 配置接口
serviceBean 配置实现类
-->
<jaxws:server id="userService" address="/userService"
serviceClass="cn.itcast.cxf.service.IUserService">
<jaxws:serviceBean>
<bean class="cn.itcast.cxf.service.UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server> </beans>

applicationContext.xml

访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl

4.整合Spring测试,编写客户端

1).编写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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--
serviceClass 服务接口
address 服务访问地址
-->
<jaxws:client id="userServiceClient"
serviceClass="cn.itcast.cxf.service.IUserService"
address="http://localhost:9998/cxf_ws_spring/services/userService" >
<!-- 来源消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</jaxws:inInterceptors>
<!-- 输出消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>

客户端的配置

2)测试类编写

 package cxf_ws_spring;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.cxf.service.IUserService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class JAXWS_Spring_Test {
@Autowired
private IUserService proxy; @Test
public void testCXF() {
System.out.println(proxy.sayHello("我是程序员"));
}
}

测试类