在 spring 引导中响应自定义代码

Responding With custom code in spring boot

我正在使用 spring 引导创建 Web 服务器。现在作为响应,我想在 api 命中 server.I 想要创建我自己的状态代码时发送我的状态代码。我不想发送 401,而是发送 421。

Response.status(421) 像这样。

这是您要找的吗?

@GetMapping("/test")
public ResponseEntity<String> someTest() {
   return ResponseEntity
      .status(421)
      .contentType(MediaType.TEXT_PLAIN)
      .body("go away world");
}

正在测试...

$ curl -I http://localhost:8080/test
HTTP/1.1 421
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: SAMEORIGIN
Content-Type: text/plain
Content-Length: 11
Date: Tue, 03 Sep 2019 12:23:58 GMT