HttpRequest
1. HttpRequest(Node remote)
- 说明: 构造一个 HttpRequest 对象,并设置远程节点。
- 用法:
Node remoteNode = new Node("127.0.0.1", 8080); HttpRequest httpRequest = new HttpRequest(remoteNode);
2. HttpRequest()
- 说明: 创建一个无参的 HttpRequest 对象。
- 用法:
HttpRequest httpRequest = new HttpRequest();
3. close()
- 说明: 关闭此 HttpRequest 的连接。
- 用法:
httpRequest.close();
4. close(String remark)
- 说明: 带备注的关闭此 HttpRequest 的连接。
- 用法:
httpRequest.close("Connection closed due to timeout.");
5. addParam(String key, Object value)
- 说明: 向 HttpRequest 添加一个参数。
- 用法:
httpRequest.addParam("username", "user123");
6. forward(String newPath)
- 说明: 将 HttpRequest 转发到新的路径。
- 用法:
httpRequest.forward("/new/path");
7. getBodyFormat()
- 说明: 获取 HttpRequest 的请求体格式。
- 用法:
RequestBodyFormat bodyFormat = httpRequest.getBodyFormat();
8. getUserAgent()
- 说明: 获取用户代理字符串。
- 用法:
String userAgent = httpRequest.getUserAgent();
9. getHost()
- 说明: 获取请求头中的 host 字段值。
- 用法:
String host = httpRequest.getHost();
10. getClientIp()
- 说明: 获取真实的客户端 IP 地址。
- 用法:
String clientIp = httpRequest.getClientIp();
11. addHeader(String key, String value)
- 说明: 添加一个 HTTP 头字段。
- 用法:
httpRequest.addHeader("Content-Type", "application/json");
18. getDomain()
- 说明: 获取请求域名。
- 用法:
String domain = httpRequest.getDomain();
13. getBodyString()
- 说明: 获取请求体的字符串表示形式。
- 用法:
String bodyString = httpRequest.getBodyString();
18. getChannelContext()
- 说明: 获取当前 HTTP 请求的通道上下文。
- 用法:
ChannelContext channelContext = httpRequest.getChannelContext();
18. getContentLength()
- 说明: 获取请求体的长度。
- 用法:
int contentLength = httpRequest.getContentLength();
16. getCookie(String cooiename)
- 说明: 根据名称获取一个 Cookie 对象。
- 用法:
Cookie cookie = httpRequest.getCookie("session_id");
18. getCookieMap()
- 说明: 获取所有 Cookie 的映射。
- 用法:
Map<String, Cookie> cookieMap = httpRequest.getCookieMap();
18. getCookies()
- 说明: 获取所有 Cookies 的列表。
- 用法:
List<Cookie> cookies = httpRequest.getCookies();
19. getHttpConfig()
- 说明: 获取 HTTP 配置。
- 用法:
HttpConfig httpConfig = httpRequest.getHttpConfig();
20. getHttpSession()
- 说明: 获取 HTTP 会话。
- 用法:
HttpSession httpSession = httpRequest.getHttpSession();
21. getHeader(String key)
- 说明: 获取指定 HTTP 头的值。
- 用法:
String value = httpRequest.getHeader("Accept");
22. getIsAjax()
- 说明: 判断请求是否是 Ajax 请求。
- 用法:
Boolean isAjax = httpRequest.getIsAjax();
23. getIsSupportGzip()
- 说明: 判断请求是否
支持 Gzip 压缩。
- 用法:
Boolean isSupportGzip = httpRequest.getIsSupportGzip();
24. getParams()
- 说明: 获取所有请求参数的映射。
- 用法:
Map<String, Object[]> params = httpRequest.getParams();
25. getParam()
- 说明: 获取单个请求参数。
- 用法:
Object param = httpRequest.getParam("key");
26. getString(String name)
- 说明: 获取指定名称的字符串参数。
- 用法:
String value = httpRequest.getString("name");
27. getInt(String name)
- 说明: 获取指定名称的整型参数。
- 用法:
Integer intValue = httpRequest.getInt("age");
28. getRemote()
- 说明: 获取远程节点信息。
- 用法:
Node remote = httpRequest.getRemote();
29. getRequestLine()
- 说明: 获取请求行信息
- 用法:
RequestLine requestLine = httpRequest.getRequestLine();
30. logstr()
- 说明: 返回 HttpRequest 的日志字符串,通常包含请求 ID、头部信息和请求体。
- 用法:
String logString = httpRequest.logstr();
31. parseCookie(HttpConfig httpConfig)
- 说明: 解析 HttpRequest 中的 Cookie 信息。
- 用法:
httpRequest.parseCookie(httpConfig);
32. setBodyFormat(RequestBodyFormat bodyFormat)
- 说明: 设置 HttpRequest 的请求体格式。
- 用法:
httpRequest.setBodyFormat(RequestBodyFormat.JSON);
33. setBodyString(String bodyString)
- 说明: 设置 HttpRequest 的请求体内容。
- 用法:
httpRequest.setBodyString("{\"name\":\"John\"}");
34. setChannelContext(ChannelContext channelContext)
- 说明: 设置 HttpRequest 的通道上下文。
- 用法:
httpRequest.setChannelContext(channelContext);
35. setCharset(String charset)
- 说明: 设置 HttpRequest 的字符集。
- 用法:
httpRequest.setCharset("UTF-8");
36. setContentLength(int contentLength)
- 说明: 设置 HttpRequest 的内容长度。
- 用法:
httpRequest.setContentLength(1024);
37. setCookieMap(Map<String, Cookie> cookieMap)
- 说明: 设置 HttpRequest 的 Cookie 映射。
- 用法:
httpRequest.setCookieMap(cookieMap);
38. setCookies(List<Cookie> cookies)
- 说明: 设置 HttpRequest 的 Cookies 列表。
- 用法:
httpRequest.setCookies(cookies);
39. setHeaders(Map<String, String> headers)
- 说明: 设置 HttpRequest 的头部信息。
- 用法:
Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); httpRequest.setHeaders(headers);
40. setHttpConfig(HttpConfig httpConfig)
- 说明: 设置 HttpRequest 的 HTTP 配置。
- 用法:
httpRequest.setHttpConfig(httpConfig);
41. setHttpSession(HttpSession httpSession)
- 说明: 设置 HttpRequest 的 HTTP 会话。
- 用法:
httpRequest.setHttpSession(httpSession);
42. setIsAjax(Boolean isAjax)
- 说明: 设置 HttpRequest 是否为 Ajax 请求。
- 用法:
httpRequest.setIsAjax(true);
43. setIsSupportGzip(Boolean isSupportGzip)
- 说明: 设置 HttpRequest 是否支持 Gzip 压缩。
- 用法:
httpRequest.setIsSupportGzip(true);
44. setParams(Map<String, Object[]> params)
- 说明: 设置 HttpRequest 的参数映射。
- 用法:
Map<String, Object[]> params = new HashMap<>(); params.put("key", new Object[]{"value"}); httpRequest.setParams(params);
45. setRemote(Node remote)
- 说明: 设置 HttpRequest 的远程节点信息。
- 用法:
httpRequest.setRemote(new Node("127.0.0.1", 8080));
46. setRequestLine(RequestLine requestLine)
- 说明: 设置 HttpRequest 的请求行。
- 用法:
RequestLine requestLine = new RequestLine("GET", "/api/data", "HTTP/1.1"); httpRequest.setRequestLine(requestLine);
47. toString()
- 说明: 返回 HttpRequest 对象的字符串表示。
- 用法:
String httpRequestString = httpRequest.toString();
48. isClosed()
- 说明: 检查 HttpRequest 是否已关闭。
- 用法:
boolean isClosed = httpRequest.isClosed();
49. setClosed(boolean closed)
- 说明: 设置 HttpRequest 的关闭状态。
- 用法:
httpRequest.setClosed(true);
50. getConnection()
- 说明: 获取 HttpRequest 的连接信息
。
- 用法:
String connection = httpRequest.getConnection();
51. setConnection(String connection)
- 说明: 设置 HttpRequest 的连接信息。
- 用法:
httpRequest.setConnection("keep-alive");
52. getReferer()
- 说明: 获取请求头中的 Referer 字段。
- 用法:
String referer = httpRequest.getReferer();
53. isNeedForward()
- 说明: 检查 HttpRequest 是否需要转发。
- 用法:
boolean needForward = httpRequest.isNeedForward();
54. setNeedForward(boolean needForward)
- 说明: 设置 HttpRequest 是否需要转发。
- 用法:
httpRequest.setNeedForward(true);
55. isForward()
- 说明: 检查 HttpRequest 是否正在转发。
- 用法:
boolean isForwarding = httpRequest.isForward();
56. setForward(boolean isForward)
- 说明: 设置 HttpRequest 的转发状态。
- 用法:
httpRequest.setForward(true);
57. getRequestMap()
返回一份可修改的 Map<String, Object>,合并查询参数、已解码的表单参数与 JSON 对象正文。JSON 同名字段覆盖查询参数;不会修改原来的参数表。getParam()、getObject()、getLong() 等原有方法保持原语义。
Map<String, Object> parameters = request.getRequestMap();
long id = ParameterValidator.id(parameters.get("id"), "id");
String title = ParameterValidator.text(parameters.get("title"), "title", 200);
RespBodyVo result = service.update(id, title);
return TioRequestContext.getResponse().setJson(result);
- 空正文只返回查询/表单参数;重复查询参数沿用
getParam()的首值规则。 application/x-www-form-urlencoded和multipart/form-data使用框架已经解码的参数,不将原始正文当作 JSON。- 其他非空正文按 JSON 对象解析;数组、标量、null 或非法 JSON 抛出
ParameterValidationException,由应用统一转换为 400。需要文本或二进制正文时直接使用getBodyString()或getBodyBytes()。 - JSON 解析使用框架配置的
Jsonprovider;默认 provider 需要 fastjson2 运行时依赖。 - 此方法不设业务正文长度上限。框架通过
HttpConfig.maxLengthOfPostBody控制请求体字节数,tio-boot 的http.multipart.max-request-size会设置这个值;http.multipart.max-file-size对应maxLengthOfMultiBody。这些配置读取整数,单位为字节。请求大小控制属于框架解码层。
在 Handler 中校验参数
nexus.io.tio.utils.validator.ParameterValidator 是无状态校验工具,不持有 HTTP 请求:
| 方法 | 用途 |
|---|---|
text(value, name, max, required) | 检查字符串类型、去除首尾空白、必填与最大长度 |
id(value, name) | 校验正整数,支持十进制字符串与整数值 |
longValue(value, name, min, max, fallback) | 检查整数格式和范围;拒绝小数与溢出 |
choice(value, name, fallback, options...) | 检查枚举值 |
object(value, name) | 检查 Map 类型及字符串键;null 返回空 Map |
array(value, name, max) | 检查 List 类型和元素数量;null 返回空 List |
require(condition, message) | 检查附加的参数约束 |
校验失败抛出 ParameterValidationException,建议在 Handler 的公共异常处理处返回 HTTP 400。它不代表身份验证或业务权限检查:租户、对象归属、余额与状态变更仍应由拦截器和 Service 分别处理。
