在SpringBoot 2.1中确定传入RequestEntity的HTTP版本 [英] Determine HTTP Version of incoming RequestEntity in SpringBoot 2.1

查看:31
本文介绍了在SpringBoot 2.1中确定传入RequestEntity的HTTP版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Spring中的传入HTTP请求中获取HTTP版本(特别是Spring Boot 2.1.4).表示在RFC2616的请求行"中定义为"HTTP版本"的 HTTP 1.1 部分:

I am trying to fetch the HTTP Version from an incoming http request in Spring (Spring Boot 2.1.4 specifically). Meaning the HTTP 1.1 part as defined as "HTTP-Version" in the "Request-Line" in RFC2616: https://tools.ietf.org/html/rfc2616#section-5.1. Spring's RequestEntity seems to have all sorts of functionality to fetch url, path, headers, etc but not this. The official docs are not helping either.

考虑以下代码:

@RestController
@RequestMapping("/")
public class MyController
{
    @GetMapping("/")
    @ResponseBody
    public ResponseEntity<String> getSomething(RequestEntity requestEntity)
    {
        // access HTTP Version number of incoming RequestEntity ***HERE***

在打印输入请求的标题时,例如

When printing the headers of an incoming request like

Map<String, String> headers = requestEntity.getHeaders().toSingleValueMap();
Object[] keys = headers.keySet().toArray();

for(int i=0; i< headers.size(); i++) {
    System.out.println(keys[i].toString() + ": " + headers.get(keys[i].toString()));
}

(请原谅我的脏日志代码),我得到了ACTUAL标头,例如

(excuse my dirty logging code), I get the ACTUAL headers, such as

host: localhost:8888
connection: keep-alive
cache-control: max-age=0
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36
dnt: 1
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9,de;q=0.8

不过,似乎仍然无法从RequestEntity中剔除HTTP版本号.

Still, there seems no way to tickle the HTTP Version number out of a RequestEntity.

有没有人?

推荐答案

RequestEntity是请求的高级表示,它使您可以方便地访问对象形式的有效负载.您所追求的是请求的低级属性,通常REST端点不必理会它.

RequestEntity is a high level representation of a request that allows you to conveniently have access to the payload in object form. What you're after is a far more low level attribute of a request that normally a REST endpoint shouldn't have to bother itself with.

您可以注入HttpServletRequest实例,从而使您可以访问协议.

You can inject the HttpServletRequest instance and that gives you access to the protocol.

@GetMapping("/")
@ResponseBody    
public ResponseEntity<String> getSomething(RequestEntity requestEntity,
                                           HttpServletRequest request) {

  log.info("HTTP protocol: " + request.getProtocol());

  ...
}

这篇关于在SpringBoot 2.1中确定传入RequestEntity的HTTP版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆