java - swagger-ui.html 400 错误请求

标签 java spring-boot swagger

我已将 swagger 集成到我的 Spring Boot 项目中。所有 swagger 端点都工作正常,但是 /product/swagger-ui.html给出 400 错误。

经过一些调试,我发现两个端点之间存在冲突。

在我的 application.properties 文件中,我使用 server.contextPath=/product .

在我的 Controller 中,我认为以下映射导致了错误。

ProductRestController.java

@RestController
public class ProductRestController {

    // some autowired services

    @GetMapping("/{id}")
    public ResponseEntity<ProductDTO> getProductById(
            @Min(value = 1, message = "id {javax.validation.constraints.Min.message}") @PathVariable Long id,
            @RequestAttribute Long tenantId) {
        return ResponseEntity.ok(productService.getProductById(id, tenantId));
    }

    @PutMapping("/{id}")
    public ResponseEntity<ProductDTO> updateProduct(
            @Min(value = 1, message = "id {javax.validation.constraints.Min.message}") @PathVariable Long id,
            @RequestBody HashMap<String, Object> requestBody, @RequestAttribute Long tenantId,
            @RequestAttribute Long userId) {

        ProductDTO productDTO;
        try {
            productDTO = objectMapper.convertValue(requestBody, ProductDTO.class);
        } catch (IllegalArgumentException e) {
            throw new HttpMessageNotReadableException(e.getMessage(), e);
        }

        Set<ConstraintViolation<ProductDTO>> errors = validator.validate(productDTO, ProductDTO.UpdateProduct.class);

        if (!errors.isEmpty()) {
            throw new ConstraintViolationException(errors);
        }

        return ResponseEntity.ok(productService.updateProduct(productDTO, requestBody, id, tenantId, userId));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteProduct(
            @Min(value = 1, message = "id {javax.validation.constraints.Min.message}") @PathVariable Long id,
            @RequestAttribute Long tenantId,
            @RequestParam(required = false, name = "delete_members") boolean deleteMembers) {
        productService.deleteProduct(id, tenantId, deleteMembers);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
    }

    //other mappings
}

我调试了一下,发现HandlerExecutionChain已经把这个请求转发到了getProductById方法,然后抛出异常无法从 String 转换为 Long。

所以我删除了该映射并再次检查它是否正常工作,但这次我收到了 HTTP 405 错误。再次通过调试,我发现堆栈跟踪显示允许的方法是 PUT 和 DELETE。

然后我删除了这两个映射并进行了检查,它工作正常。

我从中了解到的是 Spring 正在采摘/product/{id}映射 /product/swagger-ui.html端点&然后由于类型不匹配而抛出错误。

问题是为什么会发生这种情况以及如何解决此问题?

编辑:在 DispatcherServlet.doDispatch 方法中捕获异常: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "swagger-ui"

删除 GET 映射后在同一方法中捕获异常: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

最佳答案

@GetMapping("/{id}")String 中给出 id 值,并且您直接尝试将 String 映射到 。尝试使用:@PathVariable String id,然后将字符串转换为 Long,如下所示:

Long longId = Long.parseLong(id);

关于java - swagger-ui.html 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49152204/

相关文章:

asp.net - 斯瓦格停止工作

java - Vaadin Treetable - 禁用蓝色突出显示

java - 在 Apple M1 Silicon 上运行 Apache Flink 1.12 作业

java - Swagger ReaderListener 未调用

java - 如何使用 JPA 从多列构造模型实例

java - apache qpid 与 spring boot 的集成

Swagger 错误 : additionalProperty: 3XX, 4XX、5XX

java - 字符串连接和附加在 foreach 中不起作用

java - 如何在准备好的语句中包含 Oracle SQL 所需的单引号

java - Spring Boot 无法解析 View