java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot WebService服务端

SpringBoot WebService服务端&客户端使用案例教程

作者:知识浅谈

这篇文章主要介绍了SpringBoot WebService服务端&客户端使用案例教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

服务端:

依赖

<!--        webservice相关依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.0</version>
        </dependency>

服务接口

package com.example.demo.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
 * @author: Yinlei
 * Package: com.example.demo.service
 * @date: 2023-10-18 8:40
 * @Description: webservice测试
 * @version: 1.0
 */
@WebService(name = "HelloWebService",
targetNamespace = "http://helloWebService.service.demo.example.com")
public interface HelloWebService {
    @WebMethod
    @WebResult(name = "resultName")
    String get(@WebParam(name = "name") String name);
}

服务接口实现类

package com.example.demo.service.Impl;
import com.example.demo.service.HelloWebService;
import org.springframework.stereotype.Service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
/**
 * @author: Yinlei
 * Package: com.example.demo.service.Impl
 * @date: 2023-10-18 8:46
 * @Description:
 * @version: 1.0
 */
@Service
@WebService(name = "HelloWebService",
        targetNamespace = "http://helloWebService.service.demo.example.com", //命名空间,一般是对应的路径反过来
        endpointInterface = "com.example.demo.service.HelloWebService")  //实现接口的地址
public class HelloWebServiceImpl implements HelloWebService {
    @Override
    public String get( String name) {
        return name;
    }
}

cxf配置类

package com.example.demo.config;
import com.example.demo.service.HelloWebService;
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.beans.factory.annotation.Autowired;
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: Yinlei
 * Package: com.example.demo.config
 * @date: 2023-10-18 10:09
 * @Description: Cxf配置类
 * @version: 1.0
 */
@Configuration
public class CxfConfig {
    @Autowired
    private HelloWebService helloWebService;
    @Bean
    public ServletRegistrationBean disServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/webService/*"); //webservice下的请求将有CXFServlet处理
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return  new SpringBus();
    }
    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloWebService);
        endpoint.publish("/helloWebservice");
        return endpoint;
    }
}

客户端

@GetMapping("/get1")
public String get1() throws MalformedURLException {
    //创建WSDL文件的URL,这个参数为暴露webervice的网址
    URL wsdlLocation = new URL("http://localhost:9000/webService/helloWebservice?wsdl");
    //创建服务名称:第一个参数为命名空间地址,第二个参数 为本地部分的命名 HelloWebServiceImplService  这个是对应的实现类名加上Service
    QName serviceName = new QName("http://helloWebService.service.demo.example.com", "HelloWebServiceImplService");
    Service service = Service.create(wsdlLocation, serviceName);
    //获取服务实现类  参数为对应的服务接口.class
    HelloWebService port = service.getPort(HelloWebService.class);
    //调用方法
    String asd = port.get("asd");
    return asd;
}

到此这篇关于SpringBoot WebService服务端&amp;客户端使用教程的文章就介绍到这了,更多相关SpringBoot WebService服务端内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文