swagger-ui.html 400 错误请求 [英] swagger-ui.html 400 bad request

查看:136
本文介绍了swagger-ui.html 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Boot 项目中集成了 swagger.所有 swagger 端点都工作正常,但 /product/swagger-ui.html 出现 400 错误.

I have integrated swagger in my spring boot project. All swagger endpoints are working fine but /product/swagger-ui.html is giving 400 error.

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

After some debugging I have found that there is conflict between two endpoints.

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

In my application.properties file, I am using server.contextPath=/product.

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

In my controller I have following mappings that I think have caused error.

ProductRestController.java

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方法,然后它抛出异常cannot cast from String to Long.

I debugged and found that HandlerExecutionChain has forwarded this request to getProductById method and then it threw exception cannot cast from String to Long.

所以我删除了那个映射并再次检查它是否有效,但这次我收到了 HTTP 405 错误.再次通过调试,我发现堆栈跟踪显示允许的方法是 PUT &删除.

So I removed that mapping and again checked if it's working but this time I got HTTP 405 error. Again by debugging I found that stack trace is showing allowed methods are PUT & DELETE.

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

Then I removed both those mappings and checked, and it's working fine.

我从中了解到的是,spring 正在为 /product/swagger-ui.html 端点选择 /product/{id} 映射 &然后由于类型不匹配而引发错误.

What I understood From this is somehow spring is picking /product/{id} mapping for /product/swagger-ui.html endpoint & then it throws error because of type mismatch.

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

Question is why is this happening and how to resolve this issue?

EDIT :在 DispatcherServlet.doDispatch 方法中捕获的异常:org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:无法将java.lang.String"类型的值转换为所需类型java.lang.Long";嵌套异常是 java.lang.NumberFormatException: For input string: "swagger-ui"

EDIT : Exception caught in DispatcherServlet.doDispatch method: 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:不支持请求方法GET"

Exception caught in same method after removing GET mapping: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

推荐答案

@GetMapping("/{id}")String<中给出 id 值/code>,而您直接尝试将 String 映射到 Long.尝试使用:@PathVariable String id 然后将你的 String 转换为 Long 如下:

@GetMapping("/{id}") gives id value in String, and you are directly trying to map String to Long. Try to use: @PathVariable String id and then convert your String to Long as following:

Long longId = Long.parseLong(id);

这篇关于swagger-ui.html 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆