SpringBoot集成WebService

1)添加依赖

 <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.3.4</version>
    <exclusions>
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-ri</artifactId>
    <version>2.3.3</version>
    <type>pom</type>
</dependency>

2)添加服务接口

public interface UserService {
    UserDTO getById(@WebParam(name="id") Integer id);
}

3)添加服务实现类

@Service
@WebService
        (
        targetNamespace = "http://www.xjs1919.com", 
        serviceName = "userService"
)
public class UserServiceImpl implements UserService {
    @Override
    public UserDTO getById(Integer id) {
        return new UserDTO(id, "hello_"+id);
    }
}

4)添加配置类

@Configuration
@Slf4j
public class WebServiceConfig {
 
    @Autowired
    private UserService userService;

    /**
     * 注入Servlet,注意beanName不能为dispatcherServlet
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
        endpoint.publish("/userService");
        return endpoint;
    }

}

5)启动应用,查看服务列表

浏览器访问:http://localhost:8888/webservice
在这里插入图片描述
点击WSDL后面的连接可以查看WSDL地址和内容:
在这里插入图片描述

6)测试

@Test
public void testGetById(){
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://localhost:8888/webservice/userService?wsdl");
    final ObjectMapper mapper = new ObjectMapper();
    try {
        Object[] objects = client.invoke("getById", 123);
        System.out.println(mapper.writeValueAsString(objects[0]));
    } catch (Exception e) {
        e.printStackTrace();;
    }
}

完整源码下载:https://github.com/xjs1919/enumdemo/tree/master/springboot-webservice

相关推荐

  1. springboot集成mybatis-plus

    2024-03-20 04:04:02       52 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 04:04:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 04:04:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 04:04:02       82 阅读
  4. Python语言-面向对象

    2024-03-20 04:04:02       91 阅读

热门阅读

  1. 2024.3.19 ARM

    2024-03-20 04:04:02       45 阅读
  2. LeetCode 2864.最大二进制奇数

    2024-03-20 04:04:02       37 阅读
  3. React理念——Fiber架构的主要原理

    2024-03-20 04:04:02       47 阅读
  4. [Vuex]中state状态

    2024-03-20 04:04:02       37 阅读
  5. CSS-DAY1

    2024-03-20 04:04:02       38 阅读
  6. 接口和抽象类的区别

    2024-03-20 04:04:02       38 阅读
  7. 安卓面试题多线程16-20

    2024-03-20 04:04:02       38 阅读
  8. MySQL面试题之基础夯实

    2024-03-20 04:04:02       39 阅读
  9. 3.19总结

    2024-03-20 04:04:02       38 阅读
  10. Latex ------基础

    2024-03-20 04:04:02       37 阅读
  11. HTTP协议

    2024-03-20 04:04:02       42 阅读
  12. 从哈希桶角度看 unordered_map 与 unordered_set 的实现

    2024-03-20 04:04:02       38 阅读