SpringBoot使用Jersey+Swagger搭建Rest服务

Swagger能够提供API文档支持,能够直观的在web浏览器上查看并调用接口,SpringBoot使用Swagger有两种选择:原生的Controller和Jersey,可是原生的Controller只能注解在实现上,这样致使了Swagger只能注解在实现上,这不符合面向接口编程的设计原则,因此咱们优先选择Jersey搭配Swagger搭建Rest微服务。
通常状况下Swagger文档咱们只在dev和test环境开启,经过@Profile注解咱们能够有选择的开启Swagger文档。

Maven 添加 Jersey 和 Swagger 支持:
org.springframework.boot spring-boot-starter-jerseygit

<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;github

@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);  
}

}web

建立 POJO:
@ApiModel("受权信息")
public class Auth {
@ApiModelProperty("用户ID")
private Long id;
@ApiModelProperty("凭据")
private String ticket;spring

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 = "AUTHAUTH", description = "受权-受权管理") @Path("/auths") @Produces(MediaType.APPLICATIONJSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface AuthResource {
@ApiOperation("获取受权信息")
@Path("/{userId}")
@GET
public Result get(
@ApiParam(value = "用户ID", required = true)
@PathParam("userId") Long userId);json

@ApiOperation("删除受权信息")  
@Path("/{userId}")  
@DELETE  
public Result<Boolean> delete(  
        @ApiParam(value = "用户ID", required = true)  
        @PathParam("userId") Long userId);

}api

建立 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("未找到的资源");  
}

}ide

Chrome 安装 Swagger 插件,Swagger API 文档如图:
http://localhost:8888/api/swagger.json
spring-boot

github代码:https://github.com/AaronSheng/SpringBoot-Jersey