【问题标题】:Customized Exception returning empty message in Spring Boot [duplicate]在 Spring Boot 中自定义异常返回空消息 [重复]
【发布时间】:2020-11-25 21:40:39
【问题描述】:

我创建了自定义异常 ProduitIntrouvableException,它扩展了 RuntimeException

@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProduitIntrouvableException extends RuntimeException {    
    public ProduitIntrouvableException(String s) {
        super(s);
    }
}

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

@RestController
public class ProductController {

    @Autowired
    private ProductDao productDao;


    @GetMapping(value="/Produits/{id}")
    public MappingJacksonValue afficherUnProduit(@PathVariable int id) throws ProduitIntrouvableException {
        Product p = productDao.findById(id);
       
       if(p==null) 
            throw new ProduitIntrouvableException("Le produit avec l'id "+id+" n'existe pas");

        SimpleBeanPropertyFilter monFiltre = SimpleBeanPropertyFilter.serializeAllExcept("prixAchat","id");

        FilterProvider listDeNosFiltres = new SimpleFilterProvider().addFilter("monFiltreDynamique", monFiltre);

        MappingJacksonValue produitsFiltres = new MappingJacksonValue(p);

        produitsFiltres.setFilters(listDeNosFiltres);

        return produitsFiltres;
    }

   
}

但是当我将字符串 "product not found" 传递给构造函数时,我收到如下的空消息,但我不知道为什么它会被忽略

{
   "timestamp": "2020-08-05T16:36:06.825+00:00",
   "status": 404,
   "error": "Not Found",
   "message": "",
   "path": "/Produits/40"
}

可能是什么原因。

Spring Boot 版本:2.3.2.RELEASE

【问题讨论】:

  • 您有控制器代码?有问题可以加吗

标签: java spring spring-boot rest exception


【解决方案1】:

来自Spring Boot 2.3 Release Notes

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

【讨论】:

    猜你喜欢
    • 2020-05-26
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2019-12-12
    • 2019-12-10
    • 2021-08-30
    相关资源
    最近更新 更多