首页 文章

是否有可能返回HttpStatus状态,而不是来自Spring MVC中的HttpStatus枚举

提问于
浏览
0

我想回来, 555 status code in response .

我已经检查了Spring框架的ResponseEntity类 .

我可以看到所有构造函数只接受来自HttpStatus枚举的特定代码 . 这可以通过以下方式实现:

return ResponseEntity.status(HttpStatus.CREATED).contentType(MediaType.TEXT_PLAIN).body("Custom string answer");

Is there any way to return status code like 555?

2 回答

  • 1

    你不能使用Spring ResponseEntity 来做到这一点 .

    但你可以随时掌握底层的 HttpServletResponse 并做一个 response.setStatus(555) .

    作为旁注,如果您的问题是"is it OK to return non-standard HTTP codes in this scenario?",答案可能是"no" .

  • 0

    你可以在Spring做到:

    ResponseEntity.status(555).body("String or obj")
    

相关问题