springboot集成webservice接口

引言:

WebService 是一种跨编程语言和跨操作系统平台的远程调用技术。

WebService平台的三大技术:XML+XSD,SOAP,WSDL

  1. XML+XSD:WebService采用HTTP协议传输数据,采用XML格式封装数据(即XML中说明调用远程服务对象的哪个方法,传递的参数是什么,以及服务对象的 返回结果是什么)。XML是WebService平台中表示数据的格式。除了易于建立和易于分析外,XML主要的优点在于它既是平台无关的,又是厂商无关 的。无关性是比技术优越性更重要的:软件厂商是不会选择一个由竞争对手所发明的技术的。
  2. SOAP:WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明 HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。SOAP提供了标准的RPC方法来调用Web Service。SOAP协议 = HTTP协议 + XML数据格式
  3. WSDL:WebService也一样,WebService客户端要调用一个WebService服务,首先要有知道这个服务的地址在哪,以及这个服务里有什么方 法可以调用,所以,WebService务器端首先要通过一个WSDL文件来说明自己家里有啥服务可以对外调用,服务是什么(服务中有哪些方法,方法接受 的参数是什么,返回值是什么),服务的网络地址用哪个url地址表示,服务通过什么方式来调用。WSDL(Web Services Description Language)就是这样一个基于XML的语言,用于描述Web Service及其函数、参数和返回值。它是WebService客户端和服务器端都 能理解的标准格式。因为是基于XML的,所以WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。一些最新的开发工具既能根据你的 Web service生成WSDL文档,又能导入WSDL文档,生成调用相应WebService的代理类代码。

大致了解Webservice是什么之后,开始整体,使用springboot框架搭建webservice服务并且暴露服务

  • 添加maven依赖:
         <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.5</version>
        </dependency>
  • 定义Webservice接口:
@WebService
public interface JobListService {

    @WebMethod
    String getList(@WebParam(name = "userId") String userId, @WebParam(name = "agentNum") Integer agentNum);
}
  • 定义接口实现类:targetNamespace 的值最好是包名反写,不是也没关系。endpointInterface 是webservice接口的地址,注意给这个类添加@Component直接注入到spring中

@WebService(targetNamespace = "http://webservice.tibom.thit.com/",
        endpointInterface = "com.thit.tibom.webservice.JobListService")
@Component
public class JobListServiceImpl implements JobListService {

    @Override
    public String getList(String userId, Integer agentNum) {
        return "请求成功";
       
    }

}

  • 定义webservice接口服务的配置类:该类的作用是将改webservice服务以jobListService的名称发布出去。

@Configuration
public class WebServiceConfig {

    @Autowired
    private JobListService jobListService;

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


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


}
  • 启动springboot项目:

访问如下目录:ip+端口/services/服务名称?wsdl
例如:http://localhost:8081/services/jobListService?wsdl

可以看到接口的定义内容:

这样服务就可以发布成功!

使用SoapUI工具或者自己编写客户端去测试服务接口:

发布了102 篇原创文章 · 获赞 49 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/SoWhatWorld/article/details/104849373