java - 添加 HttpServletResponse/HttpServletRequest 作为方法参数时 Spring 请求挂起

标签 java rest spring-mvc spring-boot

遵循 spring.io 为 creating a simple REST service 提供的示例,我遇到了一个奇怪的问题。

如果我向 localhost:8080/greeting 发出请求,则会调用问候路由并且我会收到预期的响应:

{"id":1, "content":"Hello, World!"}

如果我添加路由“/test”,然后向 localhost:8080/test 发出 HTTP GET 请求,我会得到预期的响应:

I'm a teapot

当我做两件事中的一件时,问题就出现了。首先,如果我将 HttpServletResponse 或 HttpServletRequest 作为参数添加到测试路由,并向 localhost:8080/test 发出 HTTP GET 请求,请求将挂起,路由不会被调用/执行,也许但是并不总是返回以下内容:

BODY: OK STATUS CODE: UNKNOWN 43

第二种情况是我尝试使用@Autowire 注释来克服这个问题。如果我删除 HttpServletResponse/HttpServletRequest 方法参数并将它们作为类成员 Autowiring ,我会得到相同的行为。

如果我向任何其他无效/未定义的路由发出请求,例如HTTP GET localhost:8080/undefinedroute 我收到了预期的 404。

package hello;

import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    //@Autowired
    //HttpServletRequest request;

    //@Autowired
    //HttpServletResponse response;

    @RequestMapping("/test")
    public String index() {
       return HttpStatus.I_AM_A_TEAPOT.getReasonPhrase();
    }

    //@RequestMapping("/test")
    //public String index(HttpServletResponse response) {
       //response.setStatus(HttpStatus.I_AM_A_TEAPOT.ordinal());
       //return HttpStatus.I_AM_A_TEAPOT.getReasonPhrase();
    //}

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

最佳答案

您不能 Autowiring HttpServletRequest 或 HttpServletResponse,因为这些对象是在服务器接收和处理 HTTP 请求时创建的。它们不是 Spring 应用程序上下文中的 bean。


由于这一行,您的响应状态代码为 43(未知):

response.setStatus(HttpStatus.I_AM_A_TEAPOT.ordinal()); // status 43?

ordinal()给你枚举声明的位置,而不是状态代码的值。 I_AM_A_TEAPOT 是在 HttpStatus 中声明的第 43 个枚举。请求挂起,因为 43 是无效的状态码,您的浏览器不知道如何处理它。你应该使用:

response.setStatus(HttpStatus.I_AM_A_TEAPOT.value()); // status 418

关于java - 添加 HttpServletResponse/HttpServletRequest 作为方法参数时 Spring 请求挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31982020/

相关文章:

java - Android:File()的 Assets 文件夹路径?

java - do-while 循环不会结束并使程序崩溃

用于下载文件或 zip 文件的 Java 代码

rest - Azure Media Player - 在编码过程中显示编码进度或某种消息

java - @JsonProperty 不适用于 Content-Type : application/x-www-form-urlencoded

rest - 带有执行器的 Spring Boot 应用程序

带有 @PathVariable 的 Spring MVC 带注释的 Controller 接口(interface)

java - 在 JavaFX 中使用 WebView 会破坏 Linux 上的 UI

java - 具有不同字段数量的 html 表单的域传输对象

asp.net - 设置 POST Web API 方法?