@RequestParam支持application/x-www-form-urlencoded下参数自动装配
This commit is contained in:
+32
-16
@@ -75,22 +75,9 @@ public class HttpRequestParser {
|
||||
() -> null == queryParamMap,
|
||||
this,
|
||||
() -> {
|
||||
queryParamMap = new HashMap<>();
|
||||
if (StringUtil.isEmpty(queryString)) {
|
||||
return;
|
||||
}
|
||||
String[] params = queryString.split("&");
|
||||
for (String param : params) {
|
||||
int index2 = param.indexOf("=");
|
||||
String key = "";
|
||||
String val = "";
|
||||
if (index2 > 0) {
|
||||
key = param.substring(0, index2);
|
||||
if (index2 < param.length() - 1) {
|
||||
val = param.substring(index2 + 1);
|
||||
}
|
||||
queryParamMap.put(key, val);
|
||||
}
|
||||
queryParamMap = queryStringToMap(queryString);
|
||||
if ("application/x-www-form-urlencoded".equals(getContentType())) {
|
||||
queryParamMap.putAll(queryStringToMap(getContentAsString()));
|
||||
}
|
||||
},
|
||||
() -> queryParamMap
|
||||
@@ -154,4 +141,33 @@ public class HttpRequestParser {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getHeaderValue(String name) {
|
||||
return request.headers().get(name);
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return request.headers().get("Content-Type");
|
||||
}
|
||||
|
||||
private Map<String, String> queryStringToMap(String queryString) {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
if (StringUtil.isEmpty(queryString)) {
|
||||
return result;
|
||||
}
|
||||
String[] params = queryString.split("&");
|
||||
for (String param : params) {
|
||||
int index2 = param.indexOf("=");
|
||||
String key = "";
|
||||
String val = "";
|
||||
if (index2 > 0) {
|
||||
key = param.substring(0, index2);
|
||||
if (index2 < param.length() - 1) {
|
||||
val = param.substring(index2 + 1);
|
||||
}
|
||||
result.put(key, val);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,4 +63,9 @@ public class TestController {
|
||||
fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
|
||||
context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
@GetMapping("hello3")
|
||||
public void hello3(@RequestParam("a") String a, @RequestParam("b") String b, @RequestParam(value = "c", required = false) String c) {
|
||||
log.info("a:{} b:{} c:{}", a, b, c);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user