SpringBoot使用Jersey+Swagger搭建Rest服务
2017-10-16 00:00
931 查看
SpringBoot 使用 Jersey 搭建 Rest 服务器,Swagger 提供 API 文档支持,为什么不直接用 SpringBoot 自带的 Rest 接口呢?因为 SpringBoot 自带的接口,Swagger 不能扫描其接口,只能扫描其实现。
Maven 添加 Jersey 和 Swagger 支持:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.12</version>
</dependency>
<!-- swagger 静态资源 -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>spring-bridge</artifactId>
<version>2.5.0-b34</version>
</dependency>
配置 Jersey 和 Swagger:
@Component
@EnableConfigurationProperties(SwaggerProperties.class)
public class JerseySwaggerConfig extends JerseyConfig {
@Autowired
private SwaggerProperties swaggerProperties;
@PostConstruct
private void init() {
configSwagger();
register(SwaggerSerializers.class);
register(ApiListingResource.class);
}
private void configSwagger() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setTitle(swaggerProperties.getTitle());
beanConfig.setVersion(swaggerProperties.getVersion());
beanConfig.setContact(swaggerProperties.getContact());
beanConfig.setBasePath(swaggerProperties.getBasePath());
beanConfig.setResourcePackage(swaggerProperties.getResourcePackege());
beanConfig.setScan(true);
}
}
创建 POJO:
@ApiModel("授权信息")
public class Auth {
@ApiModelProperty("用户ID")
private Long id;
@ApiModelProperty("凭据")
private String ticket;
public Auth() {
}
public Auth(Long id, String ticket) {
this.id = id;
this.ticket = ticket;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
}
创建 API:
@Api(tags = "AUTH_AUTH", description = "授权-授权管理")
@Path("/auths")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface AuthResource {
@ApiOperation("获取授权信息")
@Path("/{userId}")
@GET
public Result<Auth> get(
@ApiParam(value = "用户ID", required = true)
@PathParam("userId") Long userId);
@ApiOperation("删除授权信息")
@Path("/{userId}")
@DELETE
public Result<Boolean> delete(
@ApiParam(value = "用户ID", required = true)
@PathParam("userId") Long userId);
}
创建 API 实现:
@RestResource
public class AuthResourceImpl implements AuthResource {
@Autowired
private AuthService authService;
@Override
public Result<Auth> get(Long userId) {
return new Result<Auth>(authService.get(userId));
}
@Override
public Result<Boolean> delete(Long userId) {
throw new NotFoundException("未找到的资源");
}
}
Chrome 安装 Swagger 插件,Swagger API 文档如图:
http://localhost:8888/api/swagger.json
github代码:https://github.com/AaronSheng/SpringBoot-Jersey
Maven 添加 Jersey 和 Swagger 支持:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.12</version>
</dependency>
<!-- swagger 静态资源 -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>spring-bridge</artifactId>
<version>2.5.0-b34</version>
</dependency>
配置 Jersey 和 Swagger:
@Component
@EnableConfigurationProperties(SwaggerProperties.class)
public class JerseySwaggerConfig extends JerseyConfig {
@Autowired
private SwaggerProperties swaggerProperties;
@PostConstruct
private void init() {
configSwagger();
register(SwaggerSerializers.class);
register(ApiListingResource.class);
}
private void configSwagger() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setTitle(swaggerProperties.getTitle());
beanConfig.setVersion(swaggerProperties.getVersion());
beanConfig.setContact(swaggerProperties.getContact());
beanConfig.setBasePath(swaggerProperties.getBasePath());
beanConfig.setResourcePackage(swaggerProperties.getResourcePackege());
beanConfig.setScan(true);
}
}
创建 POJO:
@ApiModel("授权信息")
public class Auth {
@ApiModelProperty("用户ID")
private Long id;
@ApiModelProperty("凭据")
private String ticket;
public Auth() {
}
public Auth(Long id, String ticket) {
this.id = id;
this.ticket = ticket;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
}
创建 API:
@Api(tags = "AUTH_AUTH", description = "授权-授权管理")
@Path("/auths")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface AuthResource {
@ApiOperation("获取授权信息")
@Path("/{userId}")
@GET
public Result<Auth> get(
@ApiParam(value = "用户ID", required = true)
@PathParam("userId") Long userId);
@ApiOperation("删除授权信息")
@Path("/{userId}")
@DELETE
public Result<Boolean> delete(
@ApiParam(value = "用户ID", required = true)
@PathParam("userId") Long userId);
}
创建 API 实现:
@RestResource
public class AuthResourceImpl implements AuthResource {
@Autowired
private AuthService authService;
@Override
public Result<Auth> get(Long userId) {
return new Result<Auth>(authService.get(userId));
}
@Override
public Result<Boolean> delete(Long userId) {
throw new NotFoundException("未找到的资源");
}
}
Chrome 安装 Swagger 插件,Swagger API 文档如图:
http://localhost:8888/api/swagger.json
github代码:https://github.com/AaronSheng/SpringBoot-Jersey
相关文章推荐
- SpringBoot使用Swagger搭建Rest服务
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 使用RestTemplate消费spring boot的Restful服务
- 使用jersey(2.5.1)搭建java REST服务
- 使用Spring Boot&Swagger快速构建REST API并生成优美的API文档
- 01.Spring Cloud学习笔记之使用IDEA+Spring Boot快速构建Rest服务
- Spring Cloud搭建微服务架构----使用Spring boot开发web项目
- Spring Boot使用RestTemplate消费REST服务的几个问题记录
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- 利用Jersey和Google ProtoBuf 集成Spring搭建REST服务
- 使用Jersey客户端请求Spring Boot(RESTFul)服务
- SpringCloud从入门到进阶(四)——使用SpringBoot搭建微服务
- 使用Swagger生成Spring Boot REST客户端(支持Feign)(待实践)
- Gradle + Spring Boot + Jersey搭建REST开发环境
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 详解如何使用Jersey客户端请求Spring Boot(RESTFul)服务
- spring boot项目使用swagger-codegen生成服务间调用的jar包
- 使用spring boot搭建后台微服务框架