gpt4 book ai didi

java - 如何处理来自 Spring Data Rest Resource 的异常?

转载 作者:行者123 更新时间:2023-11-30 06:01:30 25 4
gpt4 key购买 nike

我在 Spring Data Rest Web 应用程序中使用 @RestResource。我想自定义我的 RestResource 的 404 错误。

我知道 ControlerAdvice、处理程序...但是它不适用于 RestResource

我已经尝试了很多事情:

  • 使用 bean 或在 application.properties 中将 setThrowExceptionIfNoHandlerFound 设置为 true
  • 在 application.properties 中禁用 spring.resources.add-mappings=false
  • 在 ControllerAdvice 上添加 basePackages 或 basePackageClass
  • 扩展 RepositoryRestExceptionHandler (但我无法覆盖它,因为方法是包私有(private)的)

我当前的源代码是:

@RestResource(path="user")
public interface UserRepository extends Repository<User, Long> {

User findById(Long id);

}

-

@EnableWebMvc
@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ErrorHandlerController extends ResponseEntityExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
ResponseEntity<?> handleNotFound(ResourceNotFoundException ex) {
ApiError apiError = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, "Internal server error");
return new ResponseEntity<>(apiError, apiError.getHttpStatus());
}

}

编辑 1(我的问题不重复):

我还尝试添加一个像这样的新的 RestControllerAdviceHandler (但它不起作用):

@RestControllerAdvice
public class RestControllerAdviceHandler {

@ExceptionHandler(Exception.class)
public ResponseEntity<String> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
return new ResponseEntity<>(" test ", HttpStatus.NOT_FOUND);
}

}

编辑2(关于spring框架源码):

经过对 Spring 源代码的更多研究,我找到了下面的代码。似乎如果没有找到实体,不会引发异常,RestResource 只是返回一个新的 ResponseEntity>(HttpStatus.NOT_FOUND))。也许用 RestResource 拦截和定制 Not Found 错误真的不可能?

/**
* <code>GET /{repository}/{id}</code> - Returns a single entity.
*
* @param resourceInformation
* @param id
* @return
* @throws HttpRequestMethodNotSupportedException
*/
@RequestMapping(value = BASE_MAPPING + "/{id}", method = RequestMethod.GET)
public ResponseEntity<Resource<?>> getItemResource(RootResourceInformation resourceInformation,
@BackendId Serializable id, final PersistentEntityResourceAssembler assembler, @RequestHeader HttpHeaders headers)
throws HttpRequestMethodNotSupportedException {

return getItemResource(resourceInformation, id).map(it -> {

PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();

return resourceStatus.getStatusAndHeaders(headers, it, entity).toResponseEntity(//
() -> assembler.toFullResource(it));

}).orElseGet(() -> new ResponseEntity<Resource<?>>(HttpStatus.NOT_FOUND));
}

最佳答案

这个问题最终在 Spring Data REST 即将发布的 3.2 版本中得到修复。

抛出了 ResourceNotFoundException 异常,现在可以正确拦截该异常。您使用 @ControllerAdvice 的解决方案将会起作用。

使用 Spring Boot 2.2.0 M2 和以下 ExceptionHandler 进行测试:

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CustomRepositoryRestExceptionHandler {

@ExceptionHandler
ResponseEntity<?> handleNotFound(ResourceNotFoundException o_O) {
// Simply re-throw the exception and let the default handling kick in, which will produce a response body.
throw o_O;
}
}

正如评论中已经发布的,问题是 https://jira.spring.io/browse/DATAREST-1143 .

关于java - 如何处理来自 Spring Data Rest Resource 的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com