服务端部分:
第一步:在Pom文件加入WebService的相关包配置,如下
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> <springboot.version>1.5.4.RELEASE</springboot.version> <mybatis-spring-boot>1.2.0</mybatis-spring-boot> <mysql-connector>5.1.40</mysql-connector> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <!-- Core Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>1.1.1.RELEASE</version> </dependency> <!-- cxf支持 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency> </dependencies>第二步:编写需要提供的服务接口
package com.demo.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import org.springframework.stereotype.Service; @SuppressWarnings("restriction") @WebService(name = "HelloWorldService", // 暴露服务名称 targetNamespace = "http://service.demo.com/"// 命名空间,一般是接口的包名倒序 ) @Service public interface HelloWorldService { @WebMethod String getHello(@WebParam(name = "message") String message); }第三步:编写对应服务的实现类
package com.demo.service; import org.springframework.stereotype.Component; @javax.jws.WebService(serviceName = "HelloWorldService", portName = "HelloWorldServiceImpl", targetNamespace = "http://service.demo.com/", endpointInterface = "com.demo.service.HelloWorldService") @Component public class HelloWorldServiceImpl implements HelloWorldService { public String getHello(String message) { return "hello" + message; } }第四步:加入webService的配置类
@Configuration public class WebServiceConfig { @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public HelloWorldServiceImpl helloWorldService() { return new HelloWorldServiceImpl(); } /** * 自定义servlet的访问 */ @Bean public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/webservice/*"); bean.setLoadOnStartup(0); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); return bean; } /** JAX-WS **/ @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), helloWorldService()); endpoint.publish("/HelloWorldService"); return endpoint; } }启动项目在浏览器中输入http://localhost:8080/webservice/HelloWorldService?wsdl 即可显示对应的wsdl的xml说明文档,说明服务发布成功 在这里服务端部分就结束了
客户端部分 通过重建动态客户端的方式调用服务
public class Test { public static void main(String[] args) { cl2(); } public static void cl2() { // 创建动态客户端 JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://localhost:8002/webservice/HelloWorldService?wsdl"); // 需要密码的情况需要加上用户名和密码 // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, // PASS_WORD)); Object[] objects = new Object[0]; try { // invoke("方法名",参数1,参数2,参数3....); objects = client.invoke("getHello", "你好。。。"); System.out.println("返回数据:" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } } }简单介绍下weiservice
Web service的概念 什么是WebService呢?从表面上看,Web Service就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web调用来实现某个功能的应用程序。从深层次上看,Web Service是一种新的Web应用程序分支,它们是自包含、自描述、模块化的应用,可以在网络(通常为Web)中被描述、发布、查找以及通过Web来调用。一旦部署以后,其他Web Service应用程序可以发现并调用它部署的服务。Web Service是基于网络的、分布式的模块化组件,它执行特定的任务,遵守具体的技术规范,这些规范使得Web Service能与其他兼容的组件进行互操作。它可以使用标准的互联网协议,像超文本传输协议HTTP和XML,将功能体现在互联网和企业内部网上。Web Service平台是一套标准,它定义了应用程序如何在Web上实现互操作性。你可以用你喜欢的任何语言,在你喜欢的任何平台上写Web Service。Web Service是构建互联网分布式系统的基本部件。”网络服务”(WebService)的本质,就是通过网络调用其他网站的资源。
Web Service 三个基本技术 Web Service通过标准通信协议,在互联网上发布有用的程序模块(以服务的方式),目前大部分是用SOAP来作通信协议。
Web Service提供一份详细的接口说明书,来帮助用户构建应用程序,这个接口说明书叫作WSDL(Web Service Description Language)。
通常已发布的WebService要注册到管理服务器,这样便于使用者查询和使用。这个是通过UDDI(Universal Discovery Description and Integration)来完成的。
