swagger-ui.html 400错误请求

时间:2018-03-07 12:39:07

标签: java spring-boot swagger

我在春季启动项目中集成了招摇。所有swagger端点都正常工作,但/product/swagger-ui.html给出400错误。

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

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

在我的控制器中,我有以下映射,我认为这会导致错误。

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&amp; DELETE

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

我的理解从这一点来看,春天正在挑选/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

3 个答案:

答案 0 :(得分:0)

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

Long longId = Long.parseLong(id);

答案 1 :(得分:0)

您是对的:通过执行/ {id},spring假设swagger-ui.html是id。如果您的baseUrl = /,则为URL:http://localhost:8080/swagger-ui.html

答案 2 :(得分:0)

虽然它是一个旧线程,但提供我的观点以防万一它对某人有帮助。

swagger URL 指向 ProductRestController,因为它没有自己的任何上下文路径。因此,要解决此问题,请尝试向 ProductRestController 添加上下文路径,例如 @RequestMapping("v1")。

那么你的 http://localhost:8080/swagger-ui.html 的 swagger URL 应该可以工作,因为它不会指向任何控制器。

相关问题