Solon Web 开发,四、请求上下文
Handler + Context 架构,是Solon Web 的基础。在 Context 里可以获取:
- 请求相关的对象与接口
- 会话状态相关的对象与接口
- 响应相关的对象与接口
或者理解所有请求与响应相关的,都在它身上。关于架构方面,可以再看看《想法与架构笔记》
1、三种获取 Context 的方式
a) 通过 Controller 获取
@Controller
public class HelloController{
@Mapping("/hello")
public String hello(Context ctx){
//可以注入 ctx:Context
return "Hello " + ctx.param("name", "world");
}
}
b) 通过 Handler 获取
Solon.start(DemoApp.class, args, app->{
app.get("/hello", ctx-> ctx.output("Hello " + ctx.param("name", "world")));
});
//或者,用以组件方式编写
@Mapping("/hello")
@Component
public class HelloHandler implements Handler{
public void handle(Context ctx) throws Throwable{
ctx.output("Hello " + ctx.param("name", "world"));
}
}
c) 直接获取
Context ctx = Context.current();
2、请求相关的接口
| 请求相关接口 | 说明 |
|---|---|
| -request()->Object | 原始请求对象 |
| -ip()->String | 获取源始请求ip(也可能是代理的ip) |
| -realIp()->String | 获取客户端真实IP |
| -isMultipart()-bool | 是否为分段内容 |
| -isMultipartFormData()->bool | 是否为分段表单数据 |
| -method()->String | 获取请求方式 |
| -protocol()->String | 获取请求协议 |
| -protocolAsUpper()->String | 获取请求协议并大写 |
| -uri()->URI | 获取请求的URI |
| -path()->String | 获取请求的URI路径 |
| -pathNew(String) | 设置新路径 |
| -pathNew()->String | 获取新路径,不存在则返回原路径 |
| -pathMap(String)->NvMap | 获取请求的URI路径变量,根据路径表达式 |
| -pathAsUpper()->String | 获取请求的URI路径并大写 |
| -userAgent()>String | 获取请求的UA |
| -url()->String | 获取请求的URL字符串 |
| -contentLength()->long | 获取内容长度 |
| -contentType()->String | 获取内容类型 |
| -queryString()->String | 获取查询字符串 |
| -accept()->String | 获取 Accept 头信息 |
| -body()->String | 获取body内容 |
| -body(String)->String | 获取body内容,并按指定字符串解码 |
| -bodyNew()->String | 获取新的body |
| -bodyNew(String) | 设置新的body |
| -bodyAsBytes()->byte[] | 获取body内容为byte[] |
| -bodyAsStream()->InputStream | 获取body内容为Stream |
| -paramValues(String)->String[] | 获取参数数组 |
| -param(String)->String | 获取参数 |
| -param(String, String)->String | 获取参数,并给定默认值 |
| -paramAsInt(String)->int | 获取参数并转为int |
| -paramAsInt(String, int)->int | 获取参数并转为int, 并给定默认值 |
| -paramAsLong(String)->long | 获取参数并转为long |
| -paramAsLong(String, long)->long | 获取参数并转为long,并给定默认值 |
| -paramAsDouble(String)->double | 获取参数并转为double |
| -paramAsDouble(String, double)->double | 获取参数并转为double,并给定默认值 |
| -paramAsDecimal(String)->BigDecimal | 获取参数并转为BigDecimal |
| -paramAsDecimal(String, BigDecimal)->BigDecimal | 获取参数并转为BigDecimal,并给定默认值 |
-paramAsBean(Class<T>)->T |
获取参数并转为Bean |
| -paramMap()->NvMap | 获取所有参数并转为map |
-paramsMap()->Map<String, List<String>> |
获取所有参数并转为Map |
| -paramSet(String, String) | 设置参数 |
| -paramsAdd(String, String) | 添加参数 |
-files(String)->List<UploadedFile> |
获取上传文件,可能有多个 |
| -file(String)->UploadedFile | 获取上传文件,第一个 |
| -cookie(String)->String | 获取 cookie |
| -cookie(String, String)->String | 获取 cookie, 并给定默认值 |
| -cookieMap()->NvMap | 获取 cookieMap |
| -header(String)->String | 获取 header |
| -header(String, String)->String | 获取 header,并给定默认值 |
3、响应相关的接口
| 响应相关接口 | 说明 |
|---|---|
| -response()->Object | 原始响应对象 |
| -charset(String) | 设置字符集 |
| -contentType(String) | 设置内容类型 |
| -contentTypeNew() | 获取设置的新内容类型 |
| -output(byte[]) | 输出 字节数组 |
| -output(InputStream) | 输出 流对象 |
| -output(String) | 输出 字符串 |
| -output(Throwable) | 输出 异常对象 |
| -outputAsJson(String) | 输出为json文本 |
| -outputAsHtml(String) | 输出为html文本 |
| -outputAsFile(DownloadedFile) | 输出为文件 |
| -outputAsFile(File) | 输出为文件 |
| -outputStream()->OutputStream | 获取输出流 |
| -headerSet(String, String) | 设置 header |
| -headerAdd(String, String) | 添加 header |
| -cookieSet(String, String) | 设置 cookie |
| -cookieSet(String, String, int) | 设置 cookie |
| -cookieSet(String, String, String, int) | 设置 cookie |
| -cookieSet(String, String, String, String, int) | 设置 cookie |
| -cookieRemove(String) | 移徐 cookie |
| -redirect(String) | 302跳转地址 |
| -redirect(String, int) | 跳转地址 |
| -forward(String) | 转发地址 |
| -status() | 获取输出状态 |
| -status(int) | 设置输出状态 |
4、会话相关的接口
| 会话相关接口 | 说明 |
|---|---|
| -sessionState()->SessionState | 获取 sessionState |
| -sessionId()->String | 获取 sessionId |
| -session(String)->Object | 获取 session 状态 |
| -session(String, T)->T | 获取 session 状态(类型转换,存在风险) |
| -sessionAsInt(String)->int | 获取 session 状态以 int 型输出 |
| -sessionAsInt(String, int)->int | 获取 session 状态以 int 型输出, 并给定默认值 |
| -sessionAsLong(String)->long | 获取 session 状态以 long 型输出 |
| -sessionAsLong(String, long)->long | 获取 session 状态以 long 型输出, 并给定默认值 |
| -sessionAsDouble(String)->double | 获取 session 状态以 double 型输出 |
| -sessionAsDouble(String, double)->double | 获取 session 状态以 double 型输出, 并给定默认值 |
| -sessionSet(String, Object) | 设置 session 状态 |
| -sessionRemove(String) | 移除 session 状态 |
| -sessionClear() | 清空 session 状态 |
5、其它查询
| 其它相关接口 | 说明 |
|---|---|
| +current()->Context | 获取当前线程的上下文 |
| -getLocale()->Locale | 获取地区 |
| -setLocale(Locale) | 设置地区 |
| -setHandled(bool) | 设置处理状态 |
| -getHandled() | 获取处理状态 |
| -setRendered(bool) | 设置渲染状态 |
| -getRendered() | 获取渲染状态 |
| -attrMap()->Map | 获取自定义特性并转为Map |
| -attr(String)->Object | 获取上下文特性 |
| -attr(String, T)->T | 获取上下文特性,并设定默认值 |
| -attrSet(String, Object) | 设置上下文特性 |
| -attrSet(Map) | 设置上下文特性 |
| -attrClear() | 清除上下文特性 |
| -render(Object) | 渲染数据 |
| -render(String, Map) | 渲染视图 |
| -renderAndReturn(Object)->String | 渲染数据并返回 |
| -remoting()->bool | 是否为远程调用 |
| -remotingSet(bool) | 设置是否为远程调用 |
| -result:Object | 用于在处理链中透传处理结果 |
| -errors:Throwable | 用于在处理链中透传处理错误 |
| -controller()->Object | 获取当前控制器 |
| -action()->Action | 获取当前动作 |
Solon Web 开发,四、请求上下文的更多相关文章
- Solon Web 开发,十四、与Spring、Jsr330的常用注解对比
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,一、开始
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,二、开发知识准备
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,五、数据访问、事务与缓存应用
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,六、过滤器、处理、拦截器
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,七、视图模板与Mvc注解
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,八、校验、及定制与扩展
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
- Solon Web 开发,九、跨域处理
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
随机推荐
- 自动化集成:Pipeline流水语法详解
前言:该系列文章,围绕持续集成:Jenkins+Docker+K8S相关组件,实现自动化管理源码编译.打包.镜像构建.部署等操作:本篇文章主要描述Pipeline流水线用法. 一.Webhook原理 ...
- AT2686 [ARC080A] 4-adjacent 题解
Content 给定一个长度为 \(n\) 的数列 \(a\),请将其重新排列,使得 \(\forall i\in[1,n-1]\),都有 \(4\mid (a_i\cdot a_{i+1})\),或 ...
- 基于GDI和D3D的抓屏技术
GDI32Api.Direct3D屏幕截图 最近因为工作需要,认真研究了一下屏幕截图的方法. 最主要的方法有两种,一.调用windows GDI32 API函数.二.使用DirectX9.0来实现. ...
- TCP 两次握手为什么无法阻止历史连接?
摘要:在两次握手的情况下,「被动发起方」没有中间状态给「主动发起方」来阻止历史连接,导致「被动发起方」可能建立一个历史连接,造成资源浪费. 本文分享自华为云社区<TCP 两次握手为什么无法阻止历 ...
- JAVA获取指定日期的周一的日期
/** * 获取当前周的周一的日期 * @param date 传入当前日期 * @return */ public static Date getThisWeekMonday(Date date) ...
- qt5读取所有本机IP
说明 需要添加 network模块 本文介绍的函数将读取所有本机IP,包括 ipv4和ipv6 本文演示版本 qt5.14 头文件 #include <QHostAddress> #inc ...
- 【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 1046:Square Number
总时间限制: 1000ms 内存限制: 65536kB 描述 给定正整数b,求最大的整数a,满足a*(a+b) 为完全平方数 输入 多组数据,第一行T,表示数据数.对于每组数据,一行一个正整数表示b. ...
- [多线程]async异步操作的使用实例及不同策略的对比
#include <iostream> #include <thread> #include <mutex> #include <iostream> / ...