Solon Web 开发:四、认识请求上下文(Context)

Handler + Context 架构,是Solon Web 的基础。在 Context (org.noear.solon.core.handle.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 或 Filter 或 RouterInterceptor 接口方式获取

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) 直接获取(基于 ThreadLocal 实现)

Context ctx = Context.current();

2、关于 context-path 的类似效果(基于 pathNew 实现)

添加配置即可:(v1.11.2 后支持)

server.contextPath: "/test-service/"

也可以手动处理

public class DemoApp{
    public static void main(String[] args){
        Solon.start(DemoApp.class, args, app->{
            
            //使用专用过滤器
            app.filter(-99, new ContextPathFilter("/xxx/"));
            
            //使用专用过滤器(原生路径将不能访问,不建议使用)
            //app.filter(-99, new ContextPathFilter("/xxx/", true)); 
        });
    }
}

3、请求相关的接口

请求相关接口说明
-request()->Object原始请求对象
-ip()->String获取源始请求ip(也可能是代理的ip)
-realIp()->String获取客户端真实IP
-isMultipart()-bool是否为分段内容
-isMultipartFormData()->bool是否为分段表单数据
-method()->String获取请求方式
-protocol()->String获取请求协议
-protocolAsUpper()->String获取请求协议并大写
-url()->String获取请求的URL字符串
-uri()->URI获取请求的URI
-path()->String获取请求的URI路径
-pathNew(String)设置新路径
-pathNew()->String获取新路径,不存在则返回原路径
-pathMap(String)->NvMap获取请求的URI路径变量,根据路径表达式
-pathAsUpper()->String获取请求的URI路径并大写
-pathAsLower()->String获取请求的URI路径并小写
-userAgent()>String获取请求的UA
-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)添加参数
-filesMap()->Map<String,List<UploadedFile>>获取所有上传的文件
-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,并给定默认值
-headerValues(String)->String获取 header 数组
-headerMap()->NvMap获取 headerMap
-headersMap()->Map<String, List<String>>获取 headersMap

4、响应相关的接口

响应相关接口说明
-response()->Object原始响应对象
-charset(String)设置字符集
-contentType(String)设置内容类型
-contentTypeNew()获取设置的新内容类型
-render(Object)渲染数据(比如将对象渲染为 Json 并输出)
-render(String, Map)渲染视图
-renderAndReturn(Object)->String渲染数据并返回
-output(byte[])输出 字节数组
-output(InputStream)输出 流对象
-output(String)输出 字符串
-output(Throwable)输出 异常对象
-outputAsJson(String)输出为json文本
-outputAsHtml(String)输出为html文本
-outputAsFile(DownloadedFile)输出为文件
-outputAsFile(File)输出为文件
-outputStream()->OutputStream获取输出流
-flush()冲刷
-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)设置输出状态

5、会话相关的接口

会话相关接口说明
-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 状态

6、其它查询

其它相关接口说明
+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()清除上下文特性
-remoting()->bool是否为远程调用
-remotingSet(bool)设置是否为远程调用
-result:Object用于在处理链中透传处理结果
-errors:Throwable用于在处理链中透传处理错误
-controller()->Object获取当前控制器
-action()->Action获取当前动作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有什么问题吗:INFO 2023-07-22 23:43:48.754 [-main][*][o.noear.solon.Solon]: App: Plugin starting INFO 2023-07-22 23:43:48.937 [-main][*][o.noear.solon.Solon]: Session: Local session state plugin is loaded INFO 2023-07-22 23:43:49.256 [-main][*][o.noear.solon.Solon]: View: load: FreemarkerRender INFO 2023-07-22 23:43:49.258 [-main][*][o.noear.solon.Solon]: View: load: org.noear.solon.view.freemarker.FreemarkerRender INFO 2023-07-22 23:43:49.258 [-main][*][o.noear.solon.Solon]: View: mapping: .ftl=FreemarkerRender INFO 2023-07-22 23:43:49.292 [-main][*][o.noear.solon.Solon]: App: Bean scanning INFO 2023-07-22 23:43:50.099 [-main][*][o.noear.solon.Solon]: View: mapping: .html=FreemarkerRender INFO 2023-07-22 23:43:50.995 [-main][*][o.noear.solon.Solon]: Connector:main: undertow: Started ServerConnector@{HTTP/1.1,[http/1.1]}{http://localhost:8080} INFO 2023-07-22 23:43:50.995 [-main][*][o.noear.solon.Solon]: Server:main: undertow: Started (undertow 2.2.24/2.3.8) @893ms INFO 2023-07-22 23:43:50.997 [-main][*][o.noear.solon.Solon]: View: mapping: @json=StringSerializerRender#SnackSerializer INFO 2023-07-22 23:43:50.997 [-main][*][o.noear.solon.Solon]: View: mapping: @type_json=StringSerializerRender#SnackSerializer INFO 2023-07-22 23:43:56.851 [-main][*][c.c.c.InitConfig]: nginxIsRun:false INFO 2023-07-22 23:43:56.899 [-main][*][c.c.c.InitConfig]: runCmd:nginx -c /home/nginxWebUI/nginx.conf INFO 2023-07-22 23:43:57.055 [-main][*][c.c.c.InitConfig]: _ _ __ __ __ __ ____ ____ ____ _ (_)____ _ __| | / /___ / /_ / / / // _/ / __ \ / __ `// // __ \ | |/_/| | /| / // _ \ / __ \ / / / / / / / / / // /_/ // // / / /_> < | |/ |/ // __// /_/ // /_/ /_/ / /_/ /_/ \__, //_//_/ /_//_/|_| |__/|__/ \___//_.___/ \____//___/ /____/
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值