java - 当代码为 400 时,使用 Swagger @ApiResponse responseContainer 不起作用

标签 java api swagger documentation-generation

在 Swagger Java API 中,当我使用 code=400 的 responsecontainer="List"(或“Set”)时,我没有在 Swagger-GUI 上获取响应模型。我刚刚得到数组[对象]。

具体案例如下:

@CrossOrigin
@RestController
@RequestMapping(value = "/api")
@Loggable(prepend = true, trim = false)
public class ConfigResource {

    private final ConfigResourceDelegate delegate;

    @Inject
    public ConfigResource(final ConfigResourceDelegate delegate) {
        this.delegate = delegate;
    }


    @RequestMapping(
        value = "/v1/config",
        method = PUT,
        consumes = APPLICATION_JSON_UTF8_VALUE,
        produces = APPLICATION_JSON_UTF8_VALUE
    )
    @ApiResponses(value = {@ApiResponse(code=202,message = "ACCEPTED" ),
        @ApiResponse(code=200,response = Rejection.class, responseContainer 
        = "Set", message = "BAD_REQUEST"),
        @ApiResponse(code=500, message = "INTERNAL_SERVER_ERROR")})
    public ResponseEntity<?> putConfig(final @RequestBody ConfigDto 
    configDto){
    return delegate.putConfig(riskConfigDto);
    }
}

这是拒绝类:

public class Rejection {


private Long id;


private RejectionDTO rejection;


private String originMessage;

public Rejection() {
}

public Long getId() {
    return id;
}

public RejectionDTO getRejection() {
    return rejection;
}

public String getOriginMessage() {
    return originMessage;
}

public void setId(Long id) {
    this.id = id;
}

public void setRejection(RejectionDTO rejection) {
    this.rejection = rejection;
}

public void setOriginMessage(String originMessage) {
    this.originMessage = originMessage;
}
}

所以通常我应该在 swagger UI 的 [] 之间放置这个模型。但是,我得到了 Array[Object]:

See screen capture

最佳答案

为了使您的示例正常工作,您需要更改通配符的返回值 ResponseEntity<?> ,到一个具体的类,ResponseEntity<List<Rejection>> 。另外,您需要更改 responseContainerList来自Set .

@RequestMapping(
        value = "/v1/config",
        method = PUT,
        consumes = APPLICATION_JSON_UTF8_VALUE,
        produces = APPLICATION_JSON_UTF8_VALUE
    )
    @ApiResponses(value = {@ApiResponse(code=202,message = "ACCEPTED" ),
        @ApiResponse(code=200,response = Rejection.class, responseContainer 
        = "List", message = "BAD_REQUEST"),
        @ApiResponse(code=500, message = "INTERNAL_SERVER_ERROR")})
    public ResponseEntity<List<Rejection>> putConfig(final @RequestBody ConfigDto 
    configDto){
    return delegate.putConfig(riskConfigDto);
    }

关于java - 当代码为 400 时,使用 Swagger @ApiResponse responseContainer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46327663/

相关文章:

swagger - 如何使用 swagger api 生成 swagger,对象的additionalProperties 为 false

java - 我无法打开我的类,只能找到一个 BuildConfig 文件

java - Seam 安全性和 Prettyfaces 登录后重定向到错误的 URL

java - Kraken API在Java中的实现

php - 通过 OAuth 将 Google 联系人导入到 PHP 中的 Mysql

c# - 如何配置 Swagger/Swashbuckle 自定义序列化程序 IControllerConfiguration ASP.NET WebAPI

c# - 方法未显示在 Swagger UI(带 Swashbuckle)中,但没有错误消息

java - 公共(public)类扩展了 JPanel

java - eclipse BigInteger 编译问题

ajax - API 调用与前端直接页面调用(AJAX)