1 Star 1 Fork 0

鹞之神乐 / HttpConnection

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

HttpConnection

本项目演示了如何利用javassist在运行时动态生成class加载到ioc容器中执行aop拦截

为什么这么做?

Jsoup是在爬虫工作中非常好用的一个工具,自带的请求工具org.jsoup.helper.HttpConnection也非常好用,其对外暴露为Jsoup.connect(String url);方法; 最终执行请求都是execute();方法,所以我想在execute()时执行一些日志的操作。

相对原Jsoup优化了哪些?

1 Jsoup默认Content-Type为multipart/form-data,因此如果请求体为json字符串时会URLEncoder,从而导致服务端收到后得先URLDecoder
2 配合LoginInfo类实现cookie自动管理
3 随心所欲的AOP切入支持

如何测试?

拉取代码后执行单元测试HttpConnectionTest即可看到效果

为什么不能直接AOP拦截?

因为HttpConnection类只有一个private无参构造方法,所以无法直接AOP。

已知情况?

org.jsoup.helper.HttpConnection implements org.jsoup.Connection HttpConnection只有私有无参构造

实现思路?

1 首先创建一个抽象类me.kagura.HttpConnection实现org.jsoup.Connection接口
2 然后利用javassist在项目启动后@PostConstruct将org.jsoup.helper.HttpConnection复制一份org.jsoup.helper.HttpConnectionX
2.1 HttpConnectionX继承自抽象类me.kagura.HttpConnection
2.2 并将原private无参构造方法改为public
2.3 编译并加载该类
3 然后编写常规AOP代码即可

MIT License Copyright (c) 2018 鹞之神乐 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

SpringBoot Aop 环境下切入Jsoup或者其他私有构造类 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/Kagura/HttpConnection.git
git@gitee.com:Kagura/HttpConnection.git
Kagura
HttpConnection
HttpConnection
master

搜索帮助