spring不进debug却反回了_Spring ResponseStatusException不返回原因

I have a very simple @RestController, and I'm trying to set a custom error message. But for some reason, the message for the error is not showing up.

This is my controller:

@RestController

@RequestMapping("openPharmacy")

public class OpenPharmacyController {

@PostMapping

public String findNumberOfSurgeries(@RequestBody String skuLockRequest) {

throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "This postcode is not valid");

}

}

This is the response that I get:

{

"timestamp": "2020-06-24T17:44:20.194+00:00",

"status": 400,

"error": "Bad Request",

"message": "",

"path": "/openPharmacy/"

}

I'm passing a JSON, but I'm not validating anything, I'm just trying to set the custom message. If I change the status code, I see that on the response, but the message is always empty.

Why is this not working like expected? This is such a simple example that I can't see what may be missing. When I debug the code I can see that the error message has all the fields set. But for some reason, the message is never set on the response.

解决方案

This answer was provided by user Hassan in the comments on the original question. I'm only posting it as an answer to give it better visibility.

Basically, all you need to do is add server.error.include-message=always to your application.properties file, and now your message field should be populated.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值