zoukankan      html  css  js  c++  java
  • springboot2.1.3集成webservice及错误No operation was found with the name {...}解决办法

    1.项目使用springboot 2.1.3版本,集成webservice使用的依赖如下

     1 <parent>
     2         <groupId>org.springframework.boot</groupId>
     3         <artifactId>spring-boot-starter-parent</artifactId>
     4         <version>2.1.3.RELEASE</version>
     5         <relativePath/> <!-- lookup parent from repository -->
     6     </parent>
     7  <!-- cxf webservice  -->
     8         <dependency> 
     9             <groupId>org.apache.cxf</groupId> 
    10             <artifactId>cxf-spring-boot-starter-jaxws</artifactId> 
    11             <version>3.2.4</version> 
    12         </dependency>

    2.编写服务接口

     1 package com.example.demo.service;
     2 
     3 import javax.jws.WebMethod;
     4 import javax.jws.WebService;
     5 
     6 @WebService
     7 public interface WebServiceTest {
     8 
     9     @WebMethod     //声明暴露服务的方法,可以不写
    10     public String getMsg(String msg);
    11 }

    3.编写业务方法

    package com.example.demo.service.impl;
    
    import java.util.Random;
    
    import javax.jws.WebService;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import com.example.demo.service.WebServiceTest;
    
    @WebService(targetNamespace="http://service.demo.example.com/",
            endpointInterface="com.example.demo.service.WebServiceTest")
    @Component
    public class WebServiceTestImpl implements WebServiceTest {
    
        private static final Logger log = LoggerFactory.getLogger(WebServiceTestImpl.class);
    
        @Override
        public String getMsg(String msg) {
            log.info("客户端发来的参数:{}",msg);
            String serviceMsg = "hello,I'm server client."+new Random().nextLong();
            return serviceMsg;
        }
    
    }

    注意:接口和接口实现类不在同一包下

    4.发布服务类

     1 package com.example.demo.config;
     2 
     3 import javax.xml.ws.Endpoint;
     4 
     5 import org.apache.cxf.Bus;
     6 import org.apache.cxf.jaxws.EndpointImpl;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.context.annotation.Bean;
     9 import org.springframework.context.annotation.Configuration;
    10 
    11 import com.example.demo.service.WebServiceTest;
    12 
    13 @Configuration
    14 public class CxfConfig {
    15 
    16     @Autowired
    17     private Bus bus;
    18 
    19     @Autowired
    20     private WebServiceTest webServiceTest;
    21 
    22     @Bean
    23     public Endpoint endpoint() {
    24         EndpointImpl endpoint = new EndpointImpl(bus, webServiceTest);
    25         endpoint.publish("/user");
    26         return endpoint;
    27     }
    28 }

    5.远程访问的地址:http://ip:端口/项目路径/services/user?wsdl  来查看你的wsdl文件

    6.客户端调用

    在另一项目里,一样需要引入上面的依赖

     <!-- cxf webservice  -->
            <dependency> 
                <groupId>org.apache.cxf</groupId> 
                <artifactId>cxf-spring-boot-starter-jaxws</artifactId> 
                <version>3.2.4</version> 
            </dependency>

    然后:

    package com.example.demo;
    
    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringWebserviceClientApplicationTests {
    
        @Test
        public void contextLoads() {
        }
        @Test
        public void test() {
            // 创建动态客户端
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            Client client = dcf.createClient("http://localhost:8080/services/user?wsdl");
            Object[] objects = new Object[0];
            try {
           objects = client.invoke("getMsg", "hello,我是客户端哦!");
                System.out.println("返回数据:" + objects[0]);
            } catch (java.lang.Exception e) {
                e.printStackTrace();
            }
        }
    }

    提示:我是在springboot测试包里面测试的,你也可以直接写个main方法进行测试。

    接下来敲黑板划重点:

    1.@WebService(targetNamespace="http://service.demo.example.com/", endpointInterface="com.example.demo.service.WebServiceTest")  这个注解里面targetNamespace是一定要写的,这是指名我们暴露出去的接口在哪,不写映射不到,就会报No operation was found with the name {...}这个错误 ,endpointInterface="com.example.demo.service.WebServiceTest" 这个是告诉接口是哪个

    2.请注意targetNamespace最后面有一个斜线我就是因为没加斜线,老是报上面那个错误,真的是弄了一天,有时候一个小小的符号就够你忙活一天的了。深痛的教训啊。。。。。

    3.远程访问地址:http://xxxx/services/user?wsdl 后面加粗部分是固定写法,当然你要是重新配置了这个wsdl的访问路径,就需要使用你配置的,这里就不写怎么配置了,感觉没啥用。。。,有感兴趣的可以百度搜搜看

    最后,有不对的欢迎指出纠正!

  • 相关阅读:
    Atitit 华为基本法 attilax读后感
    Atitit 华为管理者内训书系 以奋斗者为本 华为公司人力资源管理纲要 attilax读后感
    Atitit 项目版本管理gitflow 与 Forking的对比与使用
    Atitit 管理的模式扁平化管理 金字塔 直线型管理 垂直管理 水平管理 矩阵式管理 网状式样管理 多头管理 双头管理
    Atitit 乌合之众读后感attilax总结 与读后感结构规范总结
    深入理解 JavaScript 异步系列(4)—— Generator
    深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise
    深入理解 JavaScript 异步系列(2)—— jquery的解决方案
    深入理解 JavaScript 异步系列(1)——基础
    使用 github + jekyll 搭建个人博客
  • 原文地址:https://www.cnblogs.com/zfding/p/10475397.html
Copyright © 2011-2022 走看看