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

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

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);
    }
}

在我的控制器中,当我的 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

英文:

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

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:

确定