【问题标题】:Responding With custom code in spring boot [duplicate]在 Spring Boot 中使用自定义代码进行响应 [重复]
【发布时间】:2020-01-06 05:54:47
【问题描述】:

我正在使用 Spring Boot 创建一个 Web 服务器。现在作为响应,我想在某些 api 访问服务器时发送我的状态代码。我想创建自己的状态代码。就像我想发送 421 而不是发送 401 一样。

响应状态(421) 像这样。

【问题讨论】:

  • 我想发送未预定义的状态码,如 421,它未预定义
  • 链接问题中也有回答。
  • 他们发送的是预定义代码,而不是自定义代码。
  • 阅读并尝试答案。接受的答案在第二个 sn-p 中使用了一个命名的 int 常量。
  • 所有用于发送状态的库: HttpStatus 或 HttpServletResponse ,它们具有内置的预定义状态。我们无法创建自己的

标签: java spring-boot jersey


【解决方案1】:

这就是你要找的吗?

@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 2019-09-13
    相关资源
    最近更新 更多