我们发送一个请求时,服务端的javax.servlet.http.HttpServletRequest帮我们做了什么?

在web应用程序中,后台仅仅获取请求参数是远远不够的。比如,spring的controller层,我们可以通过@RequestMapping注解一个方法使其成为web借口,我们可以通过方法参数来获取匹配获取请求的参数。 但是,如果我们想要获取更多的信息怎么办呢?
没错,用javax.servlet.http.HttpServletRequest,HttpServletRequest帮我们将请求解析封装好了已经,我们只需要拿来直接用即可。

举个例子

(HttpServletRequest在springMVC/springBoot Controller层的应用)

 @RequestMapping(value = "/**")
public ResponseEntity<Object> mockEntry(HttpServletRequest httpServletRequest) {
    return new ResponseEntity<Object>("mock match!", HttpStatus.OK);
}

在方法mockEntry中,可以通过httpServletRequest实例提供的方法获取到各种请求数据。


我们来看不同请求下,HttpServletRequest将请求封装成什么样子了。 我们将验证一下请求:

1、 GET方法,不带queryParams;

curl 'http://localhost:8089/my/url'

2、GET方法,带queryParams;

curl 'http://localhost:8089/my/url?key1=value1&key2=vlaue2'

3、POST方法,bodyType为application/x-www-form-urlencoded,不带queryParams,不带bodyParams;

curl -H 'Content-Type:application/x-www-form-urlencoded' -d '' 'http://localhost:8089/my/url'

4、POST方法,bodyType为application/x-www-form-urlencoded,不带queryParams,带bodyParams;

curl -H 'Content-Type:application/x-www-form-urlencoded' -d 'key=value' 'http://localhost:8089/my/url'

5、POST方法,bodyType为application/x-www-form-urlencoded,带queryParams,不带bodyParams;

curl -H 'Content-Type:application/x-www-form-urlencoded' -d '' 'http://localhost:8089/my/url?key=value'

6、POST方法,bodyType为application/x-www-form-urlencoded,带queryParams,带bodyParams;

curl -H 'Content-Type:application/x-www-form-urlencoded' -d 'key1=value1' 'http://localhost:8089/my/url?key2=value2'

7、POST方法,bodyType为application/json,不带queryParams,不带bodyParams;

curl -H 'Content-Type:application/json' -d '' 'http://localhost:8089/my/url'

8、POST方法,bodyType为application/json,不带queryParams,带bodyParams;

curl -H 'Content-Type:application/json' -d '{"key": "value"}' 'http://localhost:8089/my/url'

9、POST方法,bodyType为application/json,带queryParams,不带bodyParams;

curl -H 'Content-Type:application/json' -d '' 'http://localhost:8089/my/url?key=value'

10、POST方法,bodyType为application/json,带queryParams,带bodyParams;

curl -H 'Content-Type:application/json' -d '{"key": "value"}' 'http://localhost:8089/my/url?key2=value2'


准备工作

我们写一个Filter,并且写一个RequestMetadata作为数据承载类,为我们直观的输出HttpServletRequest封装了什么。

import java.io.Serializable;
import java.util.Map;

public class RequestMetadata implements Serializable {
   

    private static final long serialVersionUID = 4756023942925621584L;
    private String protocol;
    private String scheme;
    private String serverName;
    private String serverPort;
    private String servletPath;
    private String method;
    private String uri;
    private String url;
    private String contentType;
    private int contentLength;
    private Map<String, String> headerNames;
    private String queryString;
    private Map<String, String> parameterMap;
    private String body;

    public RequestMetadata() {
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public String getServerPort() {
        return serverPort;
    }

    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }

    public String getServletPath() {
        return servletPath;
    }

    public void setServletPath(String servletPath) {
        this.servletPath = servletPath;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public int getContentLength() {
        return contentLength;
    }

    public void setContentLength(int contentLength) {
        this.contentLength = contentLength;
    }

    public Map<String, String> getHeaderNames() {
        return headerNames;
    }

    public void setHeaderNames(Map<String, String> headerNames) {
        this.headerNames = headerNames;
    }

    public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    public Map<String, String> getParameterMap() {
        return parameterMap;
    }

    public void setP
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值