自定义异常在Spring Boot中返回空消息

huangapple go评论101阅读模式
英文:

Customized Exception returning empty message in Spring Boot

问题

我创建了一个自定义异常 ProduitIntrouvableException,它继承了 RuntimeException

  1. @ResponseStatus(HttpStatus.NOT_FOUND)
  2. public class ProduitIntrouvableException extends RuntimeException {
  3. public ProduitIntrouvableException(String s) {
  4. super(s);
  5. }
  6. }

在我的控制器中,当我的 pnull 时,我抛出它,如下所示:

  1. @RestController
  2. public class ProductController {
  3. @Autowired
  4. private ProductDao productDao;
  5. @GetMapping(value="/Produits/{id}")
  6. public MappingJacksonValue afficherUnProduit(@PathVariable int id) throws ProduitIntrouvableException {
  7. Product p = productDao.findById(id);
  8. if (p == null)
  9. throw new ProduitIntrouvableException("Le produit avec l'id " + id + " n'existe pas");
  10. SimpleBeanPropertyFilter monFiltre = SimpleBeanPropertyFilter.serializeAllExcept("prixAchat", "id");
  11. FilterProvider listDeNosFiltres = new SimpleFilterProvider().addFilter("monFiltreDynamique", monFiltre);
  12. MappingJacksonValue produitsFiltres = new MappingJacksonValue(p);
  13. produitsFiltres.setFilters(listDeNosFiltres);
  14. return produitsFiltres;
  15. }
  16. }

但是,当我将字符串 "product not found" 传递给构造函数时,我得到一个空消息,但我不知道为什么被忽略。

  1. {
  2. "timestamp": "2020-08-05T16:36:06.825+00:00",
  3. "status": 404,
  4. "error": "Not Found",
  5. "message": "",
  6. "path": "/Produits/40"
  7. }

可能的原因是 Spring Boot 版本为 2.3.2.RELEASE

英文:

I created my custom Exception ProduitIntrouvableException which extends RuntimeException

  1. @ResponseStatus(HttpStatus.NOT_FOUND)
  2. public class ProduitIntrouvableException extends RuntimeException {
  3. public ProduitIntrouvableException(String s) {
  4. super(s);
  5. }
  6. }

and in my controller, I throw it when my p is null as follow

  1. @RestController
  2. public class ProductController {
  3. @Autowired
  4. private ProductDao productDao;
  5. @GetMapping(value="/Produits/{id}")
  6. public MappingJacksonValue afficherUnProduit(@PathVariable int id) throws ProduitIntrouvableException {
  7. Product p = productDao.findById(id);
  8. if(p==null)
  9. throw new ProduitIntrouvableException("Le produit avec l'id "+id+" n'existe pas");
  10. SimpleBeanPropertyFilter monFiltre = SimpleBeanPropertyFilter.serializeAllExcept("prixAchat","id");
  11. FilterProvider listDeNosFiltres = new SimpleFilterProvider().addFilter("monFiltreDynamique", monFiltre);
  12. MappingJacksonValue produitsFiltres = new MappingJacksonValue(p);
  13. produitsFiltres.setFilters(listDeNosFiltres);
  14. return produitsFiltres;
  15. }
  16. }

But I am getting an empty message as follow while I pass a String "product not found" to the constructor but I don't why it is ignored

  1. {
  2. "timestamp": "2020-08-05T16:36:06.825+00:00",
  3. "status": 404,
  4. "error": "Not Found",
  5. "message": "",
  6. "path": "/Produits/40"
  7. }

what can be the reason.

Spring Boot version: 2.3.2.RELEASE

答案1

得分: 3

Spring Boot 2.3发布说明

> 默认情况下,错误消息和任何绑定错误不再默认包含在默认错误页面中。这降低了向客户端泄漏信息的风险。server.error.include-messageserver.error.include-binding-errors可以用于控制消息和绑定错误的包含。支持的值分别是alwayson-paramnever

英文:

From Spring Boot 2.3 Release Notes:

> The error message and any binding errors are no longer included in the default error page by default. This reduces the risk of leaking information to a client. server.error.include-message and server.error.include-binding-errors can be used to control the inclusion of the message and binding errors respectively. Supported values are always, on-param, and never.

huangapple
  • 本文由 发表于 2020年8月6日 00:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63269810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定