Spring 引导 API 不会 return 构建版本中的字段错误

Spring Boot API does not return field errors in the builded version

我有一个小问题,在开发过程中我创建了一个 API,它还检查 POST 值是否有效并带有注释 @Valid。

@PostMapping("/news")
@ResponseBody
public News add(@Valid @RequestBody News news){
    return repository.save(news);
}

当字段无效时,我得到一个 return,其中包含错误、字段和 defaultMessage。

我有一个带有@NotBlank 等注释的实体

一旦我将我的项目构建为 jar 文件,并且我 运行 我的项目使用此文件,我就不会再在响应中包含这些错误。

我认为这可能是因为开发模式或其他原因,但我在网上找不到任何相关信息。

我假设您正在使用 Spring Boot 的 Devtools。他们enable the inclusion of binding errors by default。这仅在开发时完成,以降低应用程序错误响应中信息泄露的风险。如果您对这种风险感到满意,可以通过将 server.error.include-binding-errors = always 添加到 application.properties.

来启用该行为