SpringBoot中使用WebService(简单的使用)

环境: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 才能访问

以上纯属个人见解,如有问题请联系本人!

  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
使用Spring Boot启动WebService时,您可能会遇到一些问题。根据引用的错误信息,您的实现类"com.hy.springboot.webservice.demo.service.impl.MyServiceImpl"没有添加"@WebService"或"@WebServiceProvider"注解。这些注解是必需的,以便Spring Boot能够正确地识别和配置您的WebService服务。 根据引用,在使用Spring Boot创建WebService时,您需要添加相应的WebService依赖。您可以在pom.xml文件添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 根据引用提供的示例,您还需要确保您的Application类上添加了"@RestController"和"@SpringBootApplication"注解,并且在该类注入了Endpoint bean。此外,您可以添加一个用于测试Endpoint的RequestMapping方法。 如果您遵循了以上步骤,但仍然无法启动WebService,请确保您的项目结构和配置正确,并检查是否有其他错误或异常。另外,您可以查看日志文件以获取更多详细的错误信息,以帮助您定位问题所在。 希望以上信息对您有所帮助,祝您成功启动Spring BootWebService!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot使用WebService(简单使用)](https://blog.csdn.net/weixin_45492007/article/details/99638002)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值