SpringBoot 搭建WebService服务端和客户端的案例分析
摘要:本文主要介绍了SpringBoot
搭建WebService
服务的服务端开发,和WebService
的客户端开发,让不熟悉WebService
开发的同学能够快速入门。
WebService
服务端开发
pom.xml
引入主要的
maven jar
包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.3.4</version>
<exclusions>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
复制代码
提前定义一个UserDto
对象
@Data
public class UserDto {
private Long id;
private String userName;
private Boolean active;
}
复制代码
申明服务接口
@WebService(name = HelloService.SERVICE_NAME,targetNamespace = HelloService.TARGET_NAMESPACE)
public interface HelloService {
/** 暴露服务名称 */
String SERVICE_NAME = "HelloService";
/** 命名空间,一般是接口的包名倒序 */
String TARGET_NAMESPACE = "http://hello.server.webservice.huzhihui.com";
@WebMethod
@WebResult(name = "String")
String hi(@WebParam(name = "userName") String userName);
@WebMethod
@WebResult(name = "UserDto")
List<UserDto> activeUsers(@WebParam(name = "userDtos") List<UserDto> userDtos);
}
复制代码
@WebParam
该注解标识传入的参数名称,必须要写
name
参数,不然生成的wsdl
参数名称是args1 args2 argsn
@WebResult
该注解标识返回的结果,必须加
name
参数,不然生成的wsdl
返回参数是return
定义接口实现
@WebService(
/** 和接口的服务名称保持一致 */
serviceName = HelloService.SERVICE_NAME,
/** 和接口的命名空间
还没有评论,来说两句吧...