欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot与WebService的简单实现

程序员文章站 2022-10-19 12:54:27
springboot整合CXF,webservice...

前言

webservice有两种实现,一种是基于xml的JAX-WS,过来过去都是xml,一种是rest风格的JAX-RS,本文只做两种方式的简单实现,不做深究

SpringBoot与JAX-WS

服务端
依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.4.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

接口

import javax.jws.WebService;
import java.util.List;

@WebService(targetNamespace = "http://localhost")
public interface UserService {

    void saveUser(User user);

    void updateUser(User user);

    void deleteUser(Integer id);

    List<User> findAllUser();

    User findById(Integer id);
}

假装实现

@Service
public class UserServiceImpl implements UserService {
    @Override
    public void saveUser(User user) {
        System.out.println("保存用户:" + user.getId() + "----" + user.getName() + "----" + user.getSex());
    }

    @Override
    public void updateUser(User user) {
        System.out.println("修改用户:" + user.getId() + "----" + user.getName() + "----" + user.getSex());
    }

    @Override
    public void deleteUser(Integer id) {
        System.out.println("删除用户:" + id);
    }

    @Override
    public List<User> findAllUser() {
        User user1 = new User(1,"1",1);
        User user2 = new User(2,"2",2);
        User user3 = new User(3,"2",3);

        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);

        return users;
    }

    @Override
    public User findById(Integer id) {

        return new User(1,"1",1);
    }
}


配置类

@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;
    }
}

客户端
没有接口的实现类和发布服务的功能,只有实体类和接口,并且与服务端一样
访问服务端:

	@Test
    void contextLoads() {
        //创建工厂对象
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        //设置访问路径
        factory.setAddress("http://localhost:8080/services/userService");
        //设置接口
        factory.setServiceClass(UserService.class);
        //创建接口的代理类
        UserService userService = (UserService) factory.create();
		
		//设置日志拦截器
        Client client = ClientProxy.getClient(userService);	
		//输入拦截器(响应)
        client.getInInterceptors().add(new LoggingInInterceptor());
        //输出拦截器(请求)
        client.getOutInterceptors().add(new LoggingOutInterceptor());

        //调用服务端接口
        userService.saveUser(new User(1l,"乌托邦",2));
    }

服务端打印输出了证明成功

SpringBoot与JAX-RS

依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>



        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.4.1</version>
        </dependency>

        <!--json转换-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.4.1</version>
        </dependency>

restful风格service,

@WebService
public interface UserService {

    @POST
    void saveUser(User user);

    @PUT
    void updateUser(User user);

    @DELETE
    @Path("/{id}")
    void deleteUser(@PathParam("id") Integer id);

    @GET
    List<User> findAllUser();

    @GET
    @Path("/{id}")
    //转json
    @Produces(MediaType.APPLICATION_JSON)
    User findById(@PathParam("id")Integer id);
}

实现类跟ws的一样
配置类

@Configuration
public class JaxRsConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private UserService userService;

    @Bean
    public Server createdServer(){
        JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();

        //设置访问地址
        endpoint.setAddress("/userService");
        //设置bus
        endpoint.setBus(bus);
        //设置实体类对象
        endpoint.setServiceBean(userService);
        return endpoint.create();
    }

}

浏览器访问,如果要别的格式的响应,就到服务端接口上改 @Produces(MediaType.APPLICATION_JSON),自行查看MediaType
SpringBoot与WebService的简单实现

本文地址:https://blog.csdn.net/this_break/article/details/109827218