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

Özk*_*Can 6 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));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)
FilterFactory.setFilter(new InternalSwaggerFilter());
Run Code Online (Sandbox Code Playgroud)

修订的示例代码片段

...
@Asynchronous
public void makeCoffee( @Suspended @ApiParam(access = "internal") final AsyncResponse asyncResponse,...)
...
Run Code Online (Sandbox Code Playgroud)

gog*_*tad 6

快进到2016年在那里招摇,用SpringMVC被替换springfox(文档,请点击这里).在springfox中可以忽略参数,但由于某些原因没有记录:

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

@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();
}
Run Code Online (Sandbox Code Playgroud)

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

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


小智 0

我认为你必须使用过滤器。这是一个示例https://github.com/wordnik/swagger-core/issues/269

也可以用java编码。