在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