[英]Is it possible to return HttpStatus status which is not from HttpStatus enum in Spring MVC
I want to return , 555 status code in response . 我想返回555状态码作为响应 。
I have checked ResponseEntity class of spring framework. 我已经检查了Spring框架的ResponseEntity类。
I can see all constructors accept only particular codes from HttpStatus enum. 我可以看到所有构造函数仅接受HttpStatus枚举中的特定代码。 This can be achieved by ,
这可以通过以下方式实现:
return ResponseEntity.status(HttpStatus.CREATED).contentType(MediaType.TEXT_PLAIN).body("Custom string answer");
Is there any way to return status code like 555? 有什么方法可以返回555等状态代码吗?
You can't do that using the Spring ResponseEntity
. 使用Spring
ResponseEntity
不能做到这一点。
But you can always get a hold of the underlying HttpServletResponse
and do a response.setStatus(555)
. 但是,您始终可以保留基础
HttpServletResponse
并执行response.setStatus(555)
。
As a side note, if your question was "is it OK to return non-standard HTTP codes in this scenario?" 附带说明一下,如果您的问题是“在这种情况下可以返回非标准的HTTP代码吗?” , the answer would've probably been "no" .
,答案可能是“否” 。
您可以在春季完成:
ResponseEntity.status(555).body("String or obj")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.