Java swagger-ui.html 400错误请求
我已经在我的spring boot项目中集成了swagger。所有的swagger端点都工作正常,但是Java swagger-ui.html 400错误请求,java,spring-boot,swagger,Java,Spring Boot,Swagger,我已经在我的spring boot项目中集成了swagger。所有的swagger端点都工作正常,但是/product/swagger ui.html给出了400个错误 经过一些调试,我发现两个端点之间存在冲突 在application.properties文件中,我使用的是server.contextPath=/product 在我的控制器中,我有以下映射,我认为这些映射导致了错误 ProductRestController.java @RestController public class
/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
}
@RestController
公共类ProductRestController{
//一些自动连线服务
@GetMapping(“/{id}”)
公共响应getProductById(
@Min(value=1,message=“id{javax.validation.constraints.Min.message}”)@PathVariable Long-id,
@RequestAttribute(长租户){
返回ResponseEntity.ok(productService.getProductById(id,tenantId));
}
@PutMapping(“/{id}”)
公共响应更新产品(
@Min(value=1,message=“id{javax.validation.constraints.Min.message}”)@PathVariable Long-id,
@RequestBody HashMap RequestBody,@RequestAttribute Long tenantId,
@RequestAttribute(长用户ID){
ProductDTO ProductDTO;
试一试{
productDTO=objectMapper.convertValue(requestBody,productDTO.class);
}捕获(IllegalArgumentException e){
抛出新的HttpMessageNodeReadableException(e.getMessage(),e);
}
Set errors=validator.validate(productDTO、productDTO.UpdateProduct.class);
如果(!errors.isEmpty()){
抛出新的ConstraintViolationException(错误);
}
返回ResponseEntity.ok(productService.updateProduct(productDTO,requestBody,id,tenantId,userId));
}
@DeleteMapping(“/{id}”)
产品的公共责任(
@Min(value=1,message=“id{javax.validation.constraints.Min.message}”)@PathVariable Long-id,
@RequestAttribute长租户,
@RequestParam(必需=false,name=“delete_members”)布尔deleteMembers){
productService.deleteProduct(id、tenantId、deleteMembers);
返回ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}
//其他映射
}
我调试并发现HandlerExecutionChain已将此请求转发到getProductById
方法,然后它抛出了exception cannot cast from String to Long
因此,我删除了该映射,并再次检查它是否正常工作,但这次我得到了HTTP405错误。再次通过调试,我发现堆栈跟踪显示允许的方法是PUT&DELETE
然后我删除了这两个映射并进行了检查,它工作正常
我从中了解到,spring不知何故正在为/product/swagger ui.html
端点选择/product/{id}
映射&然后由于类型不匹配而抛出错误
问题是为什么会发生这种情况,以及如何解决这个问题
编辑:在DispatcherServlet.doDispatch方法中捕获异常:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:未能将类型为“java.lang.String”的值转换为所需的类型“java.lang.Long”;嵌套异常是java.lang.NumberFormatException:对于输入字符串:“swagger ui”
删除GET映射后在同一方法中捕获异常:
org.springframework.web.HttpRequestMethodNotSupportedException:Request方法'GET'不受支持
@GetMapping(“/{id}”)
在String
中给出id
值,您直接尝试将字符串映射到Long
。尝试使用:@PathVariable String id
,然后将字符串转换为Long,如下所示:
Long longId = Long.parseLong(id);
您是对的:通过执行/{id},spring假定swagger-ui.html是一个id。如果您的baseUrl=/:是一个旧线程,则这是一个url,提供我的视图以防它对某人有所帮助 swagger URL指向ProductRestController,因为它没有自己的上下文路径。因此,要解决这个问题,请尝试向ProductRestController添加一个上下文路径,比如@RequestMapping(“v1”)
然后你大摇大摆的URLhttp://localhost:8080/swagger-ui.html应该可以工作,因为它不会指向任何控制器。什么是错误日志?实际上Spring没有打印错误日志,这就是我调试并发现错误的原因。我在questionHi@Kaushal28中写到,我编辑了这个问题并包含了错误日志。我尝试过,但Spring仍然选择了错误的处理程序,即它选择了/{id}处理程序,然后为
@Min
验证给出错误我也删除了该验证,现在执行getProductById中的代码并抛出错误进行解析。但是没有不明确的处理程序,它如何选择错误的处理程序?这就是我的问题所在。如果我删除所有那些/{id}映射,那么swagger端点就可以正常工作。检查是否有任何GET端点没有值=“\”2。注释所有具有Long in方法参数的端点,然后尝试只保留一个或两个端点来运行。根据错误跟踪,将字符串解析为Long似乎是错误的,这是不正确的。提供github URL以检查代码。