WebService:不同系统不同语言的数据交互,
CXF主要分为两种服务方式:
1 )JAX-WS:传输数据, xml格式,基于SOAP协议(规范:规定了xml传递数据的编码规范) ;
2 )JAX-RS:传输数据,xml或json格式.基于HTTP协议.(风格).
一:WS独立服务使用(主要看底层如何实现)
1.建立项目导包
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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.itcast.maven</groupId> 4 <artifactId>cxf_ws_helloworld</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <name>cxf_ws_helloworld</name> 7 <description>独立发布CXF服务java项目</description> 8 9 <dependencies> 10 <!-- 要进行jaxws 服务开发 --> 11 <dependency> 12 <groupId>org.apache.cxf</groupId> 13 <artifactId>cxf-rt-frontend-jaxws</artifactId> 14 <version>3.0.1</version> 15 </dependency> 16 17 <!-- 内置jetty web服务器 --> 18 <dependency> 19 <groupId>org.apache.cxf</groupId> 20 <artifactId>cxf-rt-transports-http-jetty</artifactId> 21 <version>3.0.1</version> 22 </dependency> 23 24 <!-- 日志实现 --> 25 <dependency> 26 <groupId>org.slf4j</groupId> 27 <artifactId>slf4j-log4j12</artifactId> 28 <version>1.7.12</version> 29 </dependency> 30 </dependencies> 31 </project>
2.与pom中的日志实现相对应.
1 ### direct log messages to stdout ### 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.out 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 6 7 ### direct messages to file mylog.log ### 8 log4j.appender.file=org.apache.log4j.FileAppender 9 log4j.appender.file.File=c:/mylog.log 10 log4j.appender.file.layout=org.apache.log4j.PatternLayout 11 log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 12 13 ### set log levels - for more verbose logging change 'info' to 'debug' ### 14 15 log4j.rootLogger=info, stdout
3.编写服务端程序
1)编写实体类Car 与 User
1 package cn.itcast.cxf.domain; 2 3 public class Car { 4 private Integer id; 5 private String carName; 6 private Double price; 7 8 public Integer getId() { 9 return id; 10 } 11 12 public void setId(Integer id) { 13 this.id = id; 14 } 15 16 public String getCarName() { 17 return carName; 18 } 19 20 public void setCarName(String carName) { 21 this.carName = carName; 22 } 23 24 public Double getPrice() { 25 return price; 26 } 27 28 public void setPrice(Double price) { 29 this.price = price; 30 } 31 32 @Override 33 public String toString() { 34 return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]"; 35 } 36 37 }
1 package cn.itcast.cxf.domain; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 public class User { 7 private Integer id; 8 private String username; 9 private String city; 10 11 private List<Car> cars = new ArrayList<Car>(); 12 13 public Integer getId() { 14 return id; 15 } 16 17 public void setId(Integer id) { 18 this.id = id; 19 } 20 21 public String getUsername() { 22 return username; 23 } 24 25 public void setUsername(String username) { 26 this.username = username; 27 } 28 29 public String getCity() { 30 return city; 31 } 32 33 public void setCity(String city) { 34 this.city = city; 35 } 36 37 public List<Car> getCars() { 38 return cars; 39 } 40 41 public void setCars(List<Car> cars) { 42 this.cars = cars; 43 } 44 45 }
2)编写服务(Service)
1 package cn.itcast.cxf.service; 2 3 import java.util.List; 4 5 import javax.jws.WebMethod; 6 import javax.jws.WebService; 7 8 import cn.itcast.cxf.domain.Car; 9 import cn.itcast.cxf.domain.User; 10 //使用类上面,说明标记类时WebService的服务类 11 @WebService 12 public interface IUserService { 13 //服务方法 14 @WebMethod 15 public String sayHello(String name); 16 17 @WebMethod 18 public List<Car> findCarsByUser(User user); 19 }
1 package cn.itcast.cxf.service; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.jws.WebService; 7 8 import cn.itcast.cxf.domain.Car; 9 import cn.itcast.cxf.domain.User; 10 //endpointInterface接口服务完整类名,serviceName 服务名称 11 @WebService(endpointInterface = "cn.itcast.cxf.service.IUserService", serviceName = "userService") 12 public class UserServiceImpl implements IUserService { 13 14 // 简单参数传递 15 public String sayHello(String name) { 16 return "Hello," + name; 17 } 18 19 // 复杂参数传递 20 public List<Car> findCarsByUser(User user) { 21 if ("xiaoming".equals(user.getUsername())) { 22 List<Car> cars = new ArrayList<Car>(); 23 Car car1 = new Car(); 24 car1.setId(1); 25 car1.setCarName("大众途观"); 26 car1.setPrice(200000d); 27 cars.add(car1); 28 29 Car car2 = new Car(); 30 car2.setId(2); 31 car2.setCarName("现代ix35"); 32 car2.setPrice(170000d); 33 cars.add(car2); 34 35 return cars; 36 } else { 37 return null; 38 } 39 } 40 }
3)将Uservice的服务方法,发布到网上,让其他系统调用
1 package cn.itcast.cxf.ws.server; 2 3 import javax.xml.ws.Endpoint; 4 5 import cn.itcast.cxf.service.IUserService; 6 import cn.itcast.cxf.service.UserServiceImpl; 7 8 public class WS_Server { 9 10 public static void main(String[] args) { 11 // 使用CXF 将 UserService服务 注册到网络上 12 // 1 、 服务实现对象 13 IUserService userService = new UserServiceImpl(); 14 // 2、 发布服务地址 15 String address = "http://localhost:9999/userService"; 16 // 3、 发布服务 17 Endpoint.publish(address, userService); 18 19 System.out.println("服务已经启动..."); 20 } 21 }
通过:http://ip地址:端口号/服务名称?wsdl
例如:网址: http://localhost:9999/userService?wsdl
4.编写客户端代码
1 package cn.itcast.cxf.ws.client; 2 2 3 3 import org.apache.cxf.interceptor.LoggingInInterceptor; 4 4 import org.apache.cxf.interceptor.LoggingOutInterceptor; 5 5 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; 6 6 7 7 import cn.itcast.cxf.domain.User; 8 8 import cn.itcast.cxf.service.IUserService; 9 9 10 10 public class WS_Client { 11 11 public static void main(String[] args) { 12 12 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); 13 13 jaxWsProxyFactoryBean.setServiceClass(IUserService.class); 14 14 jaxWsProxyFactoryBean.setAddress("http://localhost:9999/userService"); 15 15 16 16 // 通过拦截器 打印soap 报文信息 17 17 jaxWsProxyFactoryBean.getInInterceptors().add(new LoggingInInterceptor()); 18 18 jaxWsProxyFactoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); 19 19 20 20 IUserService proxy = (IUserService) jaxWsProxyFactoryBean.create(); 21 21 22 22 // 调用sayHello 23 23 System.out.println(proxy.sayHello("程序猿")); 24 24 25 25 // 调用findCarsByUser 26 26 User user = new User(); 27 27 user.setUsername("xiaoming"); 28 28 System.out.println(proxy.findCarsByUser(user)); 29 29 30 30 } 31 31 }