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

相關文章
相關標籤/搜索