• Springboot开发Webservice服务端和客户端


    环境说明

    Java JDK 1.8、Spring boot 2.1.6、Apache CXF 3.1.6

    POM依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.bootgroupId>
    4. <artifactId>spring-boot-starter-webartifactId>
    5. <version>2.1.6version>
    6. dependency>
    7. <dependency>
    8. <groupId>org.apache.cxfgroupId>
    9. <artifactId>cxf-rt-frontend-jaxwsartifactId>
    10. <version>3.1.6version>
    11. dependency>
    12. <dependency>
    13. <groupId>org.apache.cxfgroupId>
    14. <artifactId>cxf-rt-transports-httpartifactId>
    15. <version>3.1.6version>
    16. dependency>
    17. dependencies>

    服务端

    webService接口

    使用@WebService声明式注解声明这是一个webService接口,并设置:

    • name:服务名称
    • targetNamespace:命名空间,通常是接口的包名倒序

    注解@WebMethod是声明接口方法,可以通过operationName为接口方法设置别名,默认是方法名。

    此外他还有一个参数exclude,为true时则忽略该方法作为webService接口方法,默认为false。

    1. import javax.jws.WebMethod;
    2. import javax.jws.WebParam;
    3. import javax.jws.WebResult;
    4. import javax.jws.WebService;
    5. @WebService(name = "TestService",
    6. targetNamespace = "http://server.dandelion.com")
    7. public interface TestService {
    8. @WebMethod(operationName="getData")
    9. @WebResult(targetNamespace = "http://server.dandelion.com")
    10. String execute(@WebParam(name = "action") String action, @WebParam(name = "msg") String msg);
    11. }

    创建了webService接口TestService,其中方法execute有两个参数分别为action和msg,用注解@WebParam进行声明。

    接口实现

    创建接口实现类TestServiceImpl实现TestService接口,其中endpointInterface为webService接口的路径。

    1. import javax.jws.WebService;
    2. @WebService(serviceName = "TestService"
    3. targetNamespace = "http://server.dandelion.com",
    4. endpointInterface = "com.dandelion.server.TestService")
    5. public class TestServiceImpl implements TestService{
    6. @Override
    7. public String execute(String action, String msg) {
    8. System.out.println(String.format("action: %s", action));
    9. System.out.println(String.format("msg: %s", msg));
    10. return "1同步成功";
    11. }
    12. }
    注册接口

    创建webService的配置类WebServiceConfig,将接口服务注入容器并进行发布。

    1. import com.dandelion.server.TestServiceImpl;
    2. import org.apache.cxf.Bus;
    3. import org.apache.cxf.bus.spring.SpringBus;
    4. import org.apache.cxf.jaxws.EndpointImpl;
    5. import org.apache.cxf.transport.servlet.CXFServlet;
    6. import org.springframework.boot.web.servlet.ServletRegistrationBean;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;
    9. import javax.xml.ws.Endpoint;
    10. @Configuration
    11. public class WebServiceConfig {
    12. @Bean
    13. public ServletRegistrationBean disServlet() {
    14. ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean<>();
    15. servletRegistrationBean.setServlet(new CXFServlet());
    16. servletRegistrationBean.addUrlMappings("/webService/*");
    17. return servletRegistrationBean;
    18. }
    19. @Bean(name = Bus.DEFAULT_BUS_ID)
    20. public SpringBus springBus() {
    21. return new SpringBus();
    22. }
    23. @Bean
    24. public Endpoint endpoint(SpringBus springBus) {
    25. EndpointImpl endpoint = new EndpointImpl(springBus, new TestServiceImpl());
    26. endpoint.publish("/testService");
    27. return endpoint;
    28. }
    29. }

    项目启动效果:

    配置完成后启动项目,访问http://localhost:服务端口/webService,如下图所示说明创建webService服务成功。

    WSDL文件:访问http://localhost:服务端口/webService/testService?wsdl

    SoapUI测试

    客户端

    JAX动态调用WebService工具类
    1. import org.apache.cxf.endpoint.Client;
    2. import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    3. public class WsClientUtil {
    4. /**
    5. * 调用webservice服务
    6. * @param wsdUrl 服务地址
    7. * @param operationName 方法名称
    8. * @param params 参数
    9. * @return 服务响应结果
    10. */
    11. public static String callWebService(String wsdUrl, String operationName, Object... params){
    12. JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    13. Client client = dcf.createClient(wsdUrl);
    14. try {
    15. Object[] objects = client.invoke(operationName, params);
    16. return objects[0].toString();
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. }
    20. return "";
    21. }
    22. }

    API测试 

    1. @RestController
    2. @RequestMapping(value = "/client")
    3. public class TestClientController {
    4. ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    5. @RequestMapping(value = "test")
    6. public String test(){
    7. //在获取连接之前 还原上下文
    8. Thread.currentThread().setContextClassLoader(classLoader);
    9. // 服务地址
    10. String url = "http://localhost:9010/webService/testService?wsdl";
    11. // 方法名称
    12. String methodName = "getData";
    13. // 参数1
    14. String action = "同步用户信息";
    15. // 参数2
    16. String msg = "10012334223蒲公英不是梦1";
    17. try {
    18. // 发起请求
    19. return WsClientUtil.callWebService(url, methodName, action, msg);
    20. } catch (Exception e) {
    21. return String.format("webService调用异常:%s", e.getMessage());
    22. }
    23. }
    24. }

    效果

  • 相关阅读:
    【openGauss/MogDB的TPCH测试】
    原型模式--深复制/浅复制
    keil5 compiler6 -o1读取内部flash硬件错误的问题
    C++标准模板(STL)- 类型支持 (类型特性,)
    JAVA:以递归形式实现Pow函数功能算法(附完整源码)
    linux系统配置之单一网卡配置多个不同网段IP(centos)
    使用PE U盘在VM Workstation中安装系统
    梦开始的地方—— C语言动态内存管理(malloc+calloc+realloc+free)
    柯桥托业TOEIC考试和PETS哪个含金量高?
    树状DP(记忆化搜索)PAT甲级 1079 1090 1106
  • 原文地址:https://blog.csdn.net/weixin_42290901/article/details/139792561