springboot整合webservice服务端+客户端

本例使用cxf框架实现webservice

直接上代码:

maven依赖:

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

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

首先是服务端:

本例使用的springboot版本为2.0.3,较高版本的springboot启动会出现以下异常:

Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet

一个接口:

package com.example.demo.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

@WebService(name = "CxfService",targetNamespace = "http://webservice.demo.example.com")
public interface CxfService {
    
    @WebMethod
    @WebResult(name = "String",targetNamespace = "")
    public String sayHello(@WebParam(name = "username") String username);

}

一个实现类:

package com.example.demo.webservice;

import javax.jws.WebService;

/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-03-03 16:12
 */
@WebService(serviceName = "CxfService",targetNamespace = "http://webservice.demo.example.com",endpointInterface = "com.example.demo.webservice.CxfService")
public class CxfServiceImpl implements CxfService{

    @Override
    public String sayHello(String username) {
        return "hello:" + username;
    }
}

一个配置类:

package com.example.demo.webservice;

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-03-03 16:15
 */
@Configuration
public class CxfConfig {

    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/service/*");
    }

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

    @Bean
    public CxfService cxfService(){
        return new CxfServiceImpl();
    }

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


}

然后我们启动项目,访问该路径:http://localhost:8080/service,你的路径就是你的ip+端口+配置类中设置的ServletRegistrationBean的第二个参数,这个路径下放了你所有对外提供的服务

效果如图:

点进wdsl链接,显示的就是webservice的xml:

然后是客户端:

根据服务端暴露的wsdl,使用命令行生成客户端,包含.java文件

wsimport -s 客户端所在的项目路径 wsdl的url

或者你也可以随便生成到某个地方,然后把.java文件全部粘过去

生成过程:

 生成的代码:

 服务端调用:

package com.example.demo.client;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 功能描述:
 *
 * @author liuchaoyong
 * @version 1.0
 * @date 2019-03-04 09:49
 */
@RestController
@RequestMapping("/test")
public class TestApi {

    @GetMapping("say")
    public Object say(String username){
        CxfService_Service cxfServiceService = new CxfService_Service();
        return cxfServiceService.getCxfServiceImplPort().sayHello(username);
    }

}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wheat_Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值