搭建和对接webservice服务

本文介绍了使用Spring Boot搭建WebService服务端和客户端的方法。服务端需引入CXF依赖,编写接口、实现类和主类;客户端用Spring Boot框架,导入相关依赖,创建controller类和请求类。还总结了“ No operation was found with the name xxx…”问题的解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务端

首先搭建webservice服务端。

引入CXF的依赖,使用最新的3.5.3版本

<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.5.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.5.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.5.3</version>
    </dependency>
</dependencies>

编写TestService接口,并且加上@WebService表示这是一个服务。

package test.service;

import javax.jws.WebService;

@WebService
public interface TestService {
    /**
     * 测试webservice
     * @param test 测试参数
     * @return 测试返回值
     */
    String test(String test);
}

创建TestService的实现类,并且重写具体的服务类方法。

package test.service.impl;

import test.service.TestService;

import javax.jws.WebService;

@WebService
public class TestServiceImpl implements TestService {

    @Override
    public String test(String test) {
        return test;
    }
}

编写服务主类,绑定服务挂载的地址。运行main方法开启webservice服务。

package test;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import test.service.TestService;
import test.service.impl.TestServiceImpl;

public class ServerApplication {
    public static void main(String[] args) {
        String address = "http://127.0.0.1:8080/testService?wsdl";
        TestService testService = new TestServiceImpl();
        JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
        jaxWsServerFactoryBean.setAddress(address);
        jaxWsServerFactoryBean.setServiceBean(TestService.class);
        jaxWsServerFactoryBean.setServiceBean(testService);
        jaxWsServerFactoryBean.create();
    }
}

客户端

采用springboot框架来模拟webservice客户端。首先导入CXF以来,然后导入springboot 2.0.2.RELEASE的依赖。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.2.RELEASE</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.5.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.5.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.5.3</version>
    </dependency>

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

首先创建一个controller类,编写一个接口,表示可以从浏览器来请求该接口然后通过该接口访问webservice的服务。

package test.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import test.client.TestServiceClient;

import javax.annotation.Resource;

@RestController
public class TestController {
    @Resource
    private TestServiceClient testServiceClient;

    @RequestMapping("test/{test}")
    public String test(@PathVariable String test) throws Exception {
        String address = "http://127.0.0.1:8080/testService?wsdl";
        String methodName = "test";
        return testServiceClient.callTestService(address,methodName,test);
    }
}

编写具体的请求webservice的类,operationName表示需求请求webservice具体的服务方法名。params表示该webservice服务方法所需要的参数。

package test.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;

@Component
public class TestServiceClient {

    /**
     * 动态调用webservice接口
     * @param wsdUrl 服务地址
     * @param operationName 服务名
     * @param params 参数
     * @return 返回数据
     * @throws Exception 异常
     */
    public String callTestService(String wsdUrl, String operationName, String... params) throws Exception {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(wsdUrl);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects;
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke(method, (Object[]) params);
        return objects[0].toString();
    }

}

开启服务端和客户端后,请求http://127.0.0.1/test/字符串参数地址就可以接收到返回值。

问题总结

No operation was found with the name xxx…

该问题表示webservice服务端的接口和接口的实现类不在同一个包下面,需要在@WebService中添加targetNamespace值。也就是

@WebService(endpointInterface = "test.service.TestService",
        targetNamespace = "http://service.test/")

这里的targetNamespace值就是实现类接口的包名倒序。

参考文章

  • webservice客户端

    https://blog.youkuaiyun.com/qq_41687670/article/details/126285098

    https://blog.youkuaiyun.com/qq_27727251/article/details/101549548

  • springboot整合webservice

    https://blog.youkuaiyun.com/weixin_43935907/article/details/103001812

    https://blog.youkuaiyun.com/weixin_56995925/article/details/120416554

    https://blog.youkuaiyun.com/sujin_/article/details/83865124

  • No operation was found with the name错误

    https://www.cnblogs.com/xq357100870/p/4053776.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值