英文:
Customized Exception returning empty message in Spring Boot
问题
我创建了一个自定义异常 ProduitIntrouvableException
,它继承了 RuntimeException
。
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProduitIntrouvableException extends RuntimeException {
public ProduitIntrouvableException(String s) {
super(s);
}
}
在我的控制器中,当我的 p
为 null
时,我抛出它,如下所示:
@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。
英文:
I created my custom Exception ProduitIntrouvableException
which extends RuntimeException
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProduitIntrouvableException extends RuntimeException {
public ProduitIntrouvableException(String s) {
super(s);
}
}
and in my controller, I throw it when my p
is null
as follow
@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;
}
}
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
{
"timestamp": "2020-08-05T16:36:06.825+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/Produits/40"
}
what can be the reason.
Spring Boot version: 2.3.2.RELEASE
答案1
得分: 3
> 默认情况下,错误消息和任何绑定错误不再默认包含在默认错误页面中。这降低了向客户端泄漏信息的风险。server.error.include-message
和server.error.include-binding-errors
可以用于控制消息和绑定错误的包含。支持的值分别是always
、on-param
和never
。
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论