【问题标题】:Make @RequestHeader hidden true & not required使 @RequestHeader 隐藏为 true 且不是必需的
【发布时间】:2020-11-16 16:45:30
【问题描述】:

我有这个post请求方法:

@PostMapping(value = "personnePhysique", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<InputStreamResource> getRapportSolvabilitePPUsingPOST(
        @ApiParam(value = "rapportPP", required = true) @Valid @RequestHeader Map<String, String> apiHeader, @RequestBody RapportPP rapportPP)
        throws BusinessException {
    RapportPPDTO dto = mapper.convertValue(rapportPP, RapportPPDTO.class);
    String apiId = null;
    String apiPwd = null;
    /* Iterate over apiHeaders map */
    for (Map.Entry<String, String> entry : apiHeader.entrySet()) {
        if(entry.getKey().equals(ApiConstants.CB_API_USER_ID) && entry.getValue().equals(ApiConstants.CB_API_USER_PWD))
        {
            apiId = entry.getKey();
            apiPwd = entry.getValue();
        }
        System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
        System.out.println("apiId : " + apiId);
        System.out.println("apiPwd : " + apiPwd);
        
    }
    /* Iterate over apiHeaders map */
    try {
        ByteArrayInputStream array = this.rapportSolvabiliteService.getRapportSolvabilitePhysique(dto, apiId, apiPwd);
        return toSubscribeContractModel(array, "rapportSolvabilite.pdf");
    } catch (BusinessException e) {
        throw new BusinessException(500, message.getString(ApiConstants.TECHNICAL_ERROR_MESSAGE_KEY), e);
    }
}

如你所见,因为@ApiParam required 选项为真,所以@RequestHeader apiHeader 也是必需的, 我想知道如何使 apiHeader 隐藏和可选,因此它不会出现在我的 Swagger 文档中,并且只需要我的正文 (rapportPP)。

谢谢

【问题讨论】:

  • 我想你只需要删除@ApiParam。
  • 您好,感谢您的回复,但是如果您删除@ApiParam,它们都将是默认值(必需且不隐藏)

标签: java spring spring-boot spring-mvc swagger-ui


【解决方案1】:

我刚刚发现我们可以在几个字段上使用@ApiParam:

@PostMapping(value = "personnePhysique", produces = { MediaType.APPLICATION_OCTET_STREAM_VALUE })
public ResponseEntity<InputStreamResource> getRapportSolvabilitePPUsingPOST(
        @ApiParam(value = "rapportPP", required = true) @Valid @RequestBody RapportPP rapportPP,
        @ApiParam(value = "apiHeader", required = false, hidden = true) @RequestHeader Map<String, String> apiHeader)
        throws BusinessException {
    RapportPPDTO dto = mapper.convertValue(rapportPP, RapportPPDTO.class);
    String apiId = null;
    String apiPwd = null;
    /* Iterate over apiHeaders map */
    for (Map.Entry<String, String> entry : apiHeader.entrySet()) {
        if(entry.getKey().equals(ApiConstants.CB_API_USER_ID) && entry.getValue().equals(ApiConstants.CB_API_USER_PWD))
        {
            apiId = entry.getKey();
            apiPwd = entry.getValue();
        }
        System.out.println("Item : " + entry.getKey() + " Count : " + entry.getValue());
        System.out.println("apiId : " + apiId);
        System.out.println("apiPwd : " + apiPwd);
        
    }
    /* Iterate over apiHeaders map */
    try {
        ByteArrayInputStream array = this.rapportSolvabiliteService.getRapportSolvabilitePhysique(dto, apiId, apiPwd);
        return toSubscribeContractModel(array, "rapportSolvabilite.pdf");
    } catch (BusinessException e) {
        throw new BusinessException(500, message.getString(ApiConstants.TECHNICAL_ERROR_MESSAGE_KEY), e);
    }
}

因此,如您所见,您可以为每个指定值,如下例所示:

  • ApiHeader 已隐藏且不需要
  • rapportPP 不是隐藏的并且是必需的

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 1970-01-01
    • 2019-06-19
    • 2012-11-07
    • 1970-01-01
    • 2015-07-09
    • 2019-03-09
    相关资源
    最近更新 更多