使用自定义消息返回自定义 ResponseEntity<>(HttpStatus.BAD_REQUEST) 的最简单方法?

发布于 2025-01-18 02:54:36 字数 1075 浏览 0 评论 0 原文

这是我第一次使用 Spring Boot 开发 REST API。我想在出现 Bad Request 400 错误时返回自定义消息。

我有我的控制器:

@GetMapping("/DetailDossier/{id},{parameters}/")
public ResponseEntity<List<DetailDossierRspn>> DetailDossierQstn(
        @PathVariable(value = "id") String[] id, @PathVariable(value = "parameters") String parameters,
         throws ParseException {
    List<DetailDossierRspn> rspn = new ArrayList<>();

    WSDetailDossierService mainDAO = new WSDetailDossierService();

// If Sql result return -> List<DetailDossierRspn>

// If no sql result return null

    rspn = mainDAO.initialiserDAO(identifiant, coetb, null);
    if (rspn == null) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } else {
        return ResponseEntity.ok(rspn);
    }
}

我想要返回 400 错误的最简单方法:

ResponseEntity<>(HttpStatus.BAD_REQUEST)

使用自定义消息。

我尝试创建一个 @ControllerAdvice,但它不起作用,因为我的控制器返回一个 List

有没有一种方法可以轻松创建自定义消息?

This is the first time I develop a REST API with Spring Boot. I want to return a custom message when I have a Bad Request 400 error.

I have my controller:

@GetMapping("/DetailDossier/{id},{parameters}/")
public ResponseEntity<List<DetailDossierRspn>> DetailDossierQstn(
        @PathVariable(value = "id") String[] id, @PathVariable(value = "parameters") String parameters,
         throws ParseException {
    List<DetailDossierRspn> rspn = new ArrayList<>();

    WSDetailDossierService mainDAO = new WSDetailDossierService();

// If Sql result return -> List<DetailDossierRspn>

// If no sql result return null

    rspn = mainDAO.initialiserDAO(identifiant, coetb, null);
    if (rspn == null) {
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } else {
        return ResponseEntity.ok(rspn);
    }
}

I want the easiest way to return a 400 error:

ResponseEntity<>(HttpStatus.BAD_REQUEST)

with a custom message.

I tried to create a @ControllerAdvice, but it didn't work because my my controller return a List<Object>

Is there a way to easily create a custom message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2025-01-25 02:54:36

您需要创建一个自定义 @controllerAdvice

@component专业化的类,用于声明@ExceptionHandler ,@initbinder或@modelattribute 的方法。

扩展 responsentityExceptionHandler

@controllerAdvice 类方便的基类 希望 通过@exceptionhandler方法提供所有@requestmapping方法的集中式异常处理 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /p>

基本上类似:

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        return new ResponseEntity<>("my custom message", status);
    }

}

You need to create a custom @ControllerAdvice:

Specialization of @Component for classes that declare @ExceptionHandler, @InitBinder, or @ModelAttribute methods to be shared across multiple @Controller classes.

extending ResponseEntityExceptionHandler:

A convenient base class for @ControllerAdvice classes that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods.

Basically something like:

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        return new ResponseEntity<>("my custom message", status);
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文