簡體   English   中英

對於 400 Bad Request 沒有得到正確的錯誤信息

[英]For 400 Bad Request not getting proper error message

我們的 controller 看起來像這樣 -

@RestController
@RequestMapping(value="api")
@Validated
public class SampleController {
@RequestMapping(value = {"/test"}, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void test(
        @RequestParam(value = "testCode",required=true) String merchantCode
        ) throws Exception{
    System.out.print("This is Test");
}
}

在這里,如果我們沒有在我們的請求中指定必需的參數“testCode”,我們會得到“400 Bad Request”,但錯誤響應的消息部分仍然是空白的。

我們得到的回應——

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"","path":"/test"}

但預期是 -

{"timestamp":1592441286607,"status":400,"error":"Bad Request","message":"Required String parameter 'testCode' is not present","path":"/test"}

我們在下面使用 Spring 依賴項 -

<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-framework.version>5.2.6.RELEASE</spring-framework.version>

我所看到的是,我們得到了 MissingServletRequestParameterException,但在異常中,消息以空白(“”)的形式出現。

我剛剛用server.error.include-message=always更新了 bootstrap.yml 。 從 Spring 2.3.0 開始,Spring 的默認行為似乎發生了變化。 我們可以從更多詳細信息中參考以下鏈接https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content

application.yml 中的配置

server:
  error:
    include-message: always
    include-binding-errors: always
    include-stacktrace: on_trace_param
    include-exception: true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM