使用Swagger

时间:2016-03-03 23:10:45

标签: swagger api-doc spring-oauth2

我正在使用Spring安全性设置OAuth 2.0授权服务器。我想使用Swagger记录OAuth 2.0 API。我应该怎么做呢?

1 个答案:

答案 0 :(得分:0)

我目前正在研究它,我正在使用Springfox库来记录它。

<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency>

<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>

My Swagger Config看起来像这样:

    @Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig {

    @Bean
    public Docket documentation() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
            .useDefaultResponseMessages(false)
            .pathMapping("/")
            .apiInfo(apiInfo());
    }



    /**
     * API INFO
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx")
                .description("xxx")
                .version("xxx")                 
                .contact("xxx")
                .build();
    }
}

我的一个端点的一部分(注意:没有正确的方法来注释地图.2.4.0添加了支持,但我还没有尝试过。)

@Api(value = "xxx")
@FrameworkEndpoint
@SessionAttributes("authorizationRequest")
public class OAuthAuthorizationEndpoint {

    private AuthorizationEndpoint authorizationEndpoint;

    /**
     * @param endpoint
     *      base AuthorizationCheckpoint
     */
    public OAuthAuthorizationEndpoint(AuthorizationEndpoint endpoint){
        this.authorizationEndpoint = endpoint;
    }
@ApiOperation(value = "xxx", httpMethod = "GET", notes = "xxx")
    @ApiResponses(value = {
            @ApiResponse(code = 400, message = "Invalid input:...", response = Auth400Response.class),
            @ApiResponse(code = 200, message = "Ok", response = Auth200Response.class)
            })  
    @RequestMapping(value = "/oauth/authorize", produces = "application/json")
    @ResponseBody
    public ModelAndView authorize(@ApiIgnore Map<String, Object> model,
            @ApiParam(value = "xxx", required = true, defaultValue = "xxx") @RequestParam String response_type, 
            @ApiParam(value = "xxx", required = true) @RequestParam String client_id, 
            @ApiParam(value = "xxx", required = true) @RequestParam String redirect_uri, 
            @ApiParam(value = "xxx", required = false, defaultValue = "/") @RequestParam String realm,
            @ApiParam(value = "xxx", required = false) @RequestParam String state,
            @ApiIgnore SessionStatus sessionStatus, @ApiIgnore Principal principal){
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("response_type", response_type);
        parameters.put("client_id", client_id);
        parameters.put("realm", realm);
        parameters.put("redirect_uri", redirect_uri);
        parameters.put("state", state);
        return this.authorizationEndpoint.authorize(model, parameters, sessionStatus, principal);

这可能会对你有帮助。