windows环境下Spring Boot 集成WebService接口及调用实例

windows环境下Spring Boot 集成WebService接口及调用实例

第一步、引入Maven依赖

<!--SpringBoot 版本 -->
<dependency>
  <groupId>com.baomidou</groupId>
   <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
   <version>2.4.2</version>
</dependency>
<!--WebService 相关依赖 及其版本 -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.2.4</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.2.4</version>
</dependency>

第二步、Webservice 接口配置interface

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

/**
 * 服务端接口定义类
 * @author renhao
 */
//@WebService 注解表明该类或者接口是一个服务类,其中public、default的方法会发布到服务中
    //如果想组织某个方法发布到服务中可以使用@WebMethod(exclude=true)
@WebService
public interface MyWebservice {

    @WebMethod
    public String getData(@WebParam(name = "name") String myname);

    @WebMethod
    String getData1(@WebParam(name = "name") String myname);

    String getData2(@WebParam(name = "name") String myname);

    String getData3(@WebParam(name = "name") String myname);
}

注解解释

1、@WebService
该注解表明该类或者接口是一个服务类,其中public、default的方法会发布到服务中,在接口中的方法只能选择使用@WebMethod、不加注解,千万不能使用@WebMethod(exclude = true),否则启动会报错。

2、@WebMethod
该注解表明在WebService注解注释的接口、类中的方法,在接口中我们不能使用@WebMethod(exclude = true),但是在接口实现类中我们可以选择使用@WebMethod(exclude = true),此时表明当前的方法不会被发布到服务中

第三步、Webservice 接口实现类


import org.springframework.context.annotation.Configuration;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(
    serviceName = "MyWebservice",
        targetNamespace = "http://webservice.test.example.com",
        endpointInterface = "com.example.test.webservice.MyWebservice"
)
@Configuration
public class MyWebserviceImp implements MyWebservice{

    @Override
    public String getData(String myname) {
        return myname;
    }

    @Override
    public String getData1(String myname) {
        return null;
    }

    @Override
    public String getData2(String myname) {
        return null;
    }

    @Override
    @WebMethod(exclude = true)
    public String getData3(String myname) {
        return null;
    }
}

注解解释

1、@WebService

1、serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service。缺省值为 Java 类的简单名称 + Service。(字符串)

2、endpointInterface: 服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口

3、name:此属性的值包含XML Web Service的名称。在默认情况下,该值是实现XML Web Service的类的名称,wsdl:portType 的名称。缺省值为 Java 类或接口的非限定名称。(字符串

4、portName: wsdl:portName。缺省值为 WebService.name+Port。

5、targetNamespace:指定你想要的名称空间,认是使用接口实现类的包名的反缀

6、wsdlLocation:指定用于定义 Web Service 的 WSDL 文档的 Web 地址。Web 地址可以是相对路径或绝对路径。(字符串)
注意:实现类上可以不添加Webservice注解

2、@Configuration

从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

注意:@Configuration注解的配置类有如下要求:
@Configuration不可以是final类型;
@Configuration不可以是匿名类;
嵌套的configuration必须是静态类。

第四步、注册服务


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;

@Configuration
public class CxfMyWebservice {

//    @Autowired
//    private Bus bus;

    @Autowired
    private MyWebservice myWebservice;

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

    @Bean("cxfServletRegistration")
    public ServletRegistrationBean dispatcherServlet() {
        //注册servlet 拦截/ws 开头的请求 不设置 默认为:/services/*
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    /*
     * 发布endpoint
     */
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(),myWebservice);
        endpoint.publish("/myWebservice");//发布地址
        return endpoint;
    }
}

说明:

@Autowired
private Bus bus;
使用上述注入Bus的Bean之后,如果提示Consider defining a bean of type ‘org.apache.cxf.Bus’ in your configuration.错误,请更换如下方式:
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}

第五步、接口测试

在浏览器中输入

http://localhost:8181/ws/myWebservice/getData?wsdl

页面提示如下内容说明接口配置成功

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.test.example.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://webservice.test.example.com/" name="MyWebservice" targetNamespace="http://webservice.test.example.com">
<wsdl:import location="http://localhost:8181/ws/myWebservice/getData?wsdl=MyWebservice.wsdl" namespace="http://webservice.test.example.com/"> </wsdl:import>
<wsdl:binding name="MyWebserviceSoapBinding" type="ns1:MyWebservice">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getData1">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getData1">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getData1Response">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getData">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getData">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getDataResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="getData2">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getData2">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getData2Response">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyWebservice">
<wsdl:port binding="tns:MyWebserviceSoapBinding" name="MyWebserviceImpPort">
<soap:address location="http://localhost:8181/ws/myWebservice/getData"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值