如何在@Asynchronous jax-rs bean方法中设置Swagger忽略@Suspended AsyncResponse?

时间:2014-02-20 14:46:12

标签: ejb jax-rs swagger

Swagger-Core似乎将@Suspended最终的AsyncResponse asyncResponse成员解释为请求主体参数。这显然不是意图也不是这样。

我想告诉swagger-core忽略这个参数并将其从api-docs中排除。有什么想法吗?

这就是我的代码:

@Stateless
@Path("/coffee")
@Api(value = "/coffee", description = "The coffee service.")
public class CoffeeService
{
    @Inject
    Event<CoffeeRequest> coffeeRequestListeners;

    @GET
    @ApiOperation(value = "Get Coffee.", notes = "Get tasty coffee.")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK"),
            @ApiResponse(code = 404, message = "Beans not found."),
            @ApiResponse(code = 500, message = "Something exceptional happend.")})
    @Produces("application/json")
    @Asynchronous
    public void makeCoffee( @Suspended final AsyncResponse asyncResponse,

                             @ApiParam(value = "The coffee type.", required = true)
                             @QueryParam("type")
                             String type)
    {
        coffeeRequestListeners.fire(new CoffeeRequest(type, asyncResponse));
    }
}

更新:基于答案的解决方案

public class InternalSwaggerFilter implements SwaggerSpecFilter
{
    @Override
    public boolean isOperationAllowed(Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
        return true;
    }

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription apiDescription, Map<String, List<String>> stringListMap, Map<String, String> stringStringMap, Map<String, List<String>> stringListMap2) {
        if( parameter.paramAccess().isDefined() && parameter.paramAccess().get().equals("internal") )
            return false;
        return true;
    }
}

FilterFactory.setFilter(new InternalSwaggerFilter());

修订示例代码片段

...
@Asynchronous
public void makeCoffee( @Suspended @ApiParam(access = "internal") final AsyncResponse asyncResponse,...)
...

4 个答案:

答案 0 :(得分:6)

快进到2016年,将swagger-springmvc替换为springfox(文档可用here)。在springfox中可以忽略参数,但由于某些原因没有记录:

备选方案1 :在Docket配置中全局忽略.ignoredParameterTypes(...)的类型或带注释的类型:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .host(reverseProxyHost)
        .useDefaultResponseMessages(false)
        .directModelSubstitute(OffsetDateTime.class, String.class)
        .directModelSubstitute(Duration.class, String.class)
        .directModelSubstitute(LocalDate.class, String.class)
        .forCodeGeneration(true)
        .globalResponseMessage(RequestMethod.GET, newArrayList(
            new ResponseMessageBuilder()
                .code(200).message("Success").build()
            )
        .apiInfo(myApiInfo())
        .ignoredParameterTypes(AuthenticationPrincipal.class, Predicate.class, PathVariable.class)
        .select()
            .apis(withClassAnnotation(Api.class))
            .paths(any())
        .build();
}

Alternativ 2 :使用@ApiIgnore - 注释忽略方法中的单个参数:

@ApiOperation(value = "User details")
@RequestMapping(value = "/api/user", method = GET, produces = APPLICATION_JSON_UTF8_VALUE)
public MyUser getUser(@ApiIgnore @AuthenticationPrincipal MyUser user) {
    ...
}

答案 1 :(得分:3)

我使用您使用的相同技术解决了这个问题,但采用了不同的方法。而不是将其标记为内部我只是忽略所有具有AsyncResponse类型的参数,这样我就不需要更新所有方法中的代码来添加访问修饰符。

public class CustomSwaggerSpecFilter implements SwaggerSpecFilter {
    @Override
    public boolean isOperationAllowed(Operation operation, ApiDescription api, Map<String, List<String>> params, Map<String, String> cookies,
            Map<String, List<String>> headers) {
        return true;
    }

    @Override
    public boolean isParamAllowed(Parameter parameter, Operation operation, ApiDescription api, Map<String, List<String>> params,
            Map<String, String> cookies, Map<String, List<String>> headers) {
        if(parameter.dataType().equals("AsyncResponse")) { // ignoring AsyncResponse parameters
            return false;
        }

        return true;
    }
}

对我来说效果更好。

答案 2 :(得分:0)

我认为你必须使用过滤器。 以下是https://github.com/wordnik/swagger-core/issues/269

的示例

也可以用java编码。

答案 3 :(得分:0)

另一种方法可能是这样做。

@Bean
public SwaggerSpringMvcPlugin mvPluginOverride() {
    SwaggerSpringMvcPlugin swaggerSpringMvcPlugin = new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo());
    swaggerSpringMvcPlugin.ignoredParameterTypes(PagedResourcesAssembler.class, Pageable.class);
    return swaggerSpringMvcPlugin;
}