webservice(Y笔记)SpringBoot整合
【一点教程】Apache CXF WebService框架视频
12. SpringBoot 与 JAX-WS 整合开发
12. 1. 开发服务端( * )
12. 1. 1. 创建 maven 项目,导入坐标
<?xml version="1.0" encoding="UTF-8"?>
<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>com.yu</groupId>
<artifactId>05_jaxws_springboot_server</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Spring Boot 父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<!-- web支持,SpringMVC, Servlet支持等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--CXF的jax-ws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
12. 1. 2. 编写业务接口和实现
User
package cn.yu.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private String gender;
}
接口:
package cn.yu.service;
import cn.yu.domain.User;
import javax.jws.WebService;
import java.util.List;
/**
*
* @WebService 把该类标注为WebService接口,可能被远程客户端调用
* 注意:该注解需要在JDK1.6以上可以使用
*/
@WebService
public interface UserService {
public void saveUser(User user);
public void updateUser(User user);
public void deleteUser(Integer id);
public List<User> findAllUser();
public User findById(Integer id);
}
实现:
package cn.yu.service.impl;
import cn.yu.domain.User;
import cn.yu.service.UserService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
@Override
public void saveUser(User user) {
System.out.println("保存用户:"+user);
}
@Override
public void updateUser(User user) {
System.out.println("修改用户:"+user);
}
@Override
public void deleteUser(Integer id) {
System.out.println("删除用户:"+id);
}
@Override
public List<User> findAllUser() {
List<User> users = new ArrayList<User>();
users.add(new User(1,"张三","男"));
users.add(new User(2,"李四","男"));
users.add(new User(3,"陈六","女"));
return users;
}
@Override
public User findById(Integer id) {
return new User(1,"露西","男");
}
}
12. 1. 3. 编写 JaxWsConfig 配置类( * )
package cn.yu.config;
import cn.yu.service.UserService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class JaxWsConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
@Bean
public Endpoint createEndpoint(){
Endpoint endpoint = new EndpointImpl(bus,userService);
endpoint.publish("/userService");
return endpoint;
}
}
12. 1. 4. 编写 SpringBoot 启动类
package cn.yu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CXFSpringApplication {
public static void main(String[] args) {
SpringApplication.run(CXFSpringApplication.class,args);
}
}
12. 2. 开发客户端
<?xml version="1.0" encoding="UTF-8"?>
<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>com.yu</groupId>
<artifactId>06_jaxws_springboot_client</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- cxf对jaxws的支持 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 内置jetty web服务器 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 依赖日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
</project>
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:/axis.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
ClientDemo
package cn.yu.test;
import cn.yu.domain.User;
import cn.yu.service.UserService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class ClientDemo {
public static void main(String[] args) {
// 1 .创建工厂对象
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//2.设置参数
//2.1 设置访问路径
factory.setAddress("http://localhost:8080/services/userService");
//2.2 设置接口
factory.setServiceClass(UserService.class);
//3.创建接口的代理类对象
UserService userService = (UserService) factory.create();
//设置日志拦截器
Client client = ClientProxy.getClient(userService);
//输入拦截器(响应)
client.getInInterceptors().add(new LoggingInInterceptor());
//输出拦截器(请求)
client.getOutInterceptors().add(new LoggingOutInterceptor());
userService.saveUser(new User(1,"小张","男"));
}
}
13. SpringBoot 与 JAX-RS 整合开发
13. 1. 开发服务端
13. 1. 1. 创建 maven 项目,导入坐标
<projectxmlns= _"http://maven.apache.org/POM/ 4. 0. 0 "_
xmlns:xsi= _"http://www.w 3 .org/ 2001 /XMLSchema-instance"_
xsi:schemaLocation= _"http://maven.apache.org/POM/ 4. 0. 0
[http://maven.apache.org/xsd/maven-](http://maven.apache.org/xsd/maven-) 4. 0. 0 .xsd"_ >
<modelVersion> 4. 0. 0 </modelVersion>
<groupId>cn.sm 1234 </groupId>
<artifactId> 10 .cxf-rs-springboot-server</artifactId>
<version> 0. 0. 1 - SNAPSHOT</version>
<!--SpringBoot父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version> 1. 5. 4 .RELEASE</version>
</parent>
<dependencies>
<!--web支持,SpringMVC,Servlet支持等-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--CXF的jax-rs-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version> 3. 1. 11 </version>
</dependency>
</dependencies>
<properties>
<java.version> 1. 8 </java.version>
</properties>
</project>
13. 1. 2. 编写业务接口和实现
接口:
/*
*restful风格的接口
*http://localhost: 8888 /userService
*/
public interface UserService{
@POST //添加操作
publicvoid saveUser(Useruser);
@PUT //修改操作
publicvoid updateUser(Useruser);
@DELETE
@Path("/{id}") //http://localhost: 8888 /userService/ 10
publicvoid deleteUser(@PathParam("id")Integerid);
@GET
public List<User>findAllUser();
@GET
@Path("/{id}") //http://localhost: 8888 /userService/ 10
@Produces(MediaType. APPLICATION_JSON )
public UserfindById(@PathParam("id")Integerid);
}
实现:
@Service
public class UserServiceImpl implements UserService{
public void saveUser(Useruser){
System. out .println("保存用户:"+user);
}
public void updateUser(Useruser){
System. out .println("修改用户:"+user);
}
public void deleteUser(Integerid){
System. out .println("删除用户:"+id);
}
public List<User>findAllUser(){
List<User>users= new ArrayList<User>();
users.add( new User( 1 ,"张三","男"));
users.add( new User( 2 ,"李四","男"));
users.add( new User( 3 ,"陈六","女"));
return users;
}
public UserfindById(Integerid){
returnnew User( 1 ,"露西","男");
}
}
13. 1. 3. 编写 JaxRsConfig 配置类( * )
@Configuration
public class JaxRsConfig{
@Autowired
private Busbus;
@Autowired
private UserServiceuserService;
@Bean
public ServercreateServer(){
JAXRSServerFactoryBeanendpoint= new JAXRSServerFactoryBean();
//设置参数
//设置访问地址
endpoint.setAddress("/userService");
//设置bus
endpoint.setBus(bus);
//设置实体类对象
endpoint.setServiceBean(userService);
return endpoint.create();
}
}
13. 1. 4. 编写 SpringBoot 启动类
@SpringBootApplication
public class CXFSpringApplication{
public static void main(String[]args){
SpringApplication. _run_ (CXFSpringApplication. **class** ,args);
}
}