环境:JDK1.8、MAVEN 3.6.1、eclipse
1.什么是WebService?
Web service是一个平台独立的,低耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述、发布、发现、协调和配置这些应用程序, 用于开发分布式的互操作的应用程序
SpringBoot官网上:Spring Boot提供Web服务自动配置,因此所需的
一切都是定义您的Endpoint
。
2.添加webservice的依赖
当前的
pom
文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- webservice 基本配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!-- junit配置 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
3.创建WebServiceConfig配置类
当前的
WebServiceConfig
配置类如下:
@Configuration
public class WebServiceConfig {
//创建websocket的Endpoint
@Bean
public Endpoint getEndpoint() {
MyService serviceImpl=new MyServiceImpl();
Endpoint publish = EndpointImpl.publish("http://localhost:8080/show",serviceImpl);
return publish;
}
}
4.创建服务接口和服务实现类
当前的
MyService
接口:
/**
* @description 创建自定义的Service接口
* @author hy
* @date 2019-08-15
*/
public interface MyService {
String show();
}
当前的
MyService
的实现类:
/**
* @description 當前的实现类
* @author hy
* @date 2019-08-15
*/
public class MyServiceImpl implements MyService{
public String show() {
System.out.println("MyServiceImpl中的show方法被调用!");
return "【调用成功!】";
}
}
5.创建入口类
当前的
Application
类中的内容:
/**
* @description 使用SpringBoot创建webservice
* @author hy
* @date 2019-08-15
*/
@RestController
@SpringBootApplication
public class Application {
@Autowired
Endpoint endpoint;
@RequestMapping
public String testEndpoint() {
//org.apache.tomcat.websocket.pojo.PojoEndpointServer@6a8cd4c7
return endpoint.toString();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
6.测试
报错:class com.hy.springboot.webservice.demo.service.impl.MyServiceImpl has neither @WebService nor @WebServiceProvider annotation
说当前的MyServiceImpl必须有@WebService或者@WebServiceProvider
注解!
7.MyServiceImpl上添加注解
当前
MyServiceImpl
添加注解后为:
/**
* @description 在當前的实现类中添加注解@WebService
* @author hy
* @date 2019-08-15
*/
@WebService(endpointInterface = "com.hy.springboot.webservice.demo.service.MyService")
public class MyServiceImpl implements MyService{
public String show() {
System.out.println("MyServiceImpl中的show方法被调用!");
return "【调用成功!】";
}
}
8.再测试
报错:端点接口com.hy.springboot.webservice.demo.service.MyService没有 Web 服务注释
9.在MyService接口类中添加注解
当前添加注解后的
MyService
中的内容:
@WebService
public interface MyService {
@WebResult
@WebMethod
String show();
}
10.再再测试
报错: Failed to start component [Connector[HTTP/1.1-8080]],当前端口已被占用,修改端口为8081
11.修改端口
修改后的
WebServiceConfig
类中的getEndpoint方法
内容为:
MyService serviceImpl=new MyServiceImpl();
Endpoint publish = EndpointImpl.publish("http://localhost:8081/show",serviceImpl);
12.最后的测试
启动成功!
开始访问:http://localhost:8081/show?wsdl
显示结果这里就不显示了!
13.总结
1.使用webService的时候需先导入包
2.创建需要的服务和需要实现的服务的实现类,创建的服务接口中和实现的服务的实现类都需要添加@WebService
注解,并且实现的服务的实现类需要添加endpointInterface用来指定服务点接口
,当前的服务接口类中的方法需要添加@WebMethod
3.创建webService的配置类,当前配置类中需要使用EndpointImpl.publish(访问路劲,serviceImpl);
来创建Endpoint
4.访问的时候需要用 访问路径+?wsdl
才能访问
以上纯属个人见解,如有问题请联系本人!