在 Spring Boot 1.3.5 (Java 8) 中,我希望我的 Controller 返回自定义状态代码和状态消息。我所说的“自定义”是指不在 org.springframework.http.HttpStatus 中的状态代码。
我知道,我应该坚持使用标准代码(但我不能)。
目前我的 Controller 执行了
HttpServletResponse response
.
.
response.setStatus(255)
我得到了预期
java.lang.IllegalArgumentException: No matching constant for [255]
at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:488)
at org.springframework.test.web.servlet.result.StatusResultMatchers.getHttpStatusSeries(StatusResultMatchers.java:139)
请注意,操作可能会成功,因此在这种情况下,我想返回带有响应对象的自定义 2xx 代码(即 sendError 没有帮助)。
Google 表示这可能会在 springframework 4.3 中提供,但有人有现在实现的想法(使用 springframework 4.2.6)吗?
最佳答案
自定义 http 代码适用于 4.2。这是您的 junit 中的匹配器无法断言响应代码。应该可以在您的浏览器上运行。
关于java - 具有自定义状态代码的 Spring Boot Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38574660/