我使用的是Sping Boot 3.x,我有一个控制器定义如下:
@RestController
@RequestMapping(path = ["/my-controller"])
@Validated
class MyController {
private val log = loggerFor<MyController>()
@PutMapping("/{something}", consumes = [APPLICATION_JSON_VALUE])
@ResponseStatus(code = HttpStatus.NO_CONTENT)
fun test(
@PathVariable("something") something: String,
@Valid @RequestBody someDto: SomeDTO
) {
log.info("Received $someDto")
}
}
data class SomeDTO(val myBoolean: Boolean)
我还在application.yaml
文件中启用了问题详细信息(RFC 7807):
spring:
mvc:
problemdetails:
enabled: true
当我向/my-controller/hello
发出一个请求(在本例中我使用rest assured),其中的json主体(故意)与预期数据不匹配(myBoolean
是 not 一个有效的布尔值):
Given {
port(<port>)
contentType(JSON)
body("""{ "myBoolean" : "not a boolean"}""")
log().all()
} When {
put("/my-controller/hello")
} Then {
log().all().
statusCode(400)
}
响应正文如下所示:
{
"type": "about:blank",
"title": "Bad Request",
"status": 400,
"detail": "Failed to read request",
"instance": "/my-controller/hello"
}
我的问题是,如何将默认的type
从about:blank
更改为其他值?
1条答案
按热度按时间l7mqbcuq1#
您需要按如下方式定义@ControllerAdvice:
}
它为您的示例返回以下内容:
我使用了以下依赖项:
注意ProblemDetail是Spring框架6。Spring 6中的实现如下所示: