[中文文档] [English README]
author: xiaozhuai
email: 798047000@qq.com
JHttp是一个轻量的的http服务实现。
它可以实现基本的解析get query和post form(只支持x-www-form-urlencoded格式), 并且提供了一个简单的路由。
不要尝试在要求高效率的项目中使用它,因为它是基于线程池的,没有事件驱动的支持。(apache也是多线程模型)
但是你可以在安卓应用中使用它,来开启一个功能简单的http服务。
注意
监听小于1024的端口需要root用户权限。
如果你在安卓环境下使用,你不能在UI线程开启服务。
查看 Main.java
public class Main {
static HttpServer server;
static final int PORT = 8080;
public static void main(String[] args) {
HttpLog.setLogLevel(HttpLog.LOG_LEVEL_DEBUG); //LOG_LEVEL_INFO by default
// when 404 occured, use this to define a custom err page is a good idea
HttpConfig.addCustomPageAction(404, (request, response) -> response.append("404 Not Found, Powered by JHttp"));
try {
server = new HttpServer(PORT);
// example route, anonymous inner class instance or lambda(on java 8)
server.addRouter("/", new IHttpRouter() {
@Override
public void onRoute(HttpRequest request, HttpResponse response) {
response.append("hello");
}
});
// example regex route
server.addRouterRegex("/article/(\\w+)", (request, response) -> {
String action = request.getPathinfo().group(1); // (\\w+) maybe add, delete, read, etc...
response.append(action+" an article");
});
// example get query
server.addRouter("/user", (request, response) -> {
String user = request.get("user", "");
response.append("hello, "+user);
});
// example response file
server.addRouter("/file", (request, response) -> {
try {
response.contentType("text/plain");
response.file("/Users/xiaozhuai/Desktop/test.txt");
} catch (IOException e) {
e.printStackTrace();
}
});
//example Controller, "?<action>" marked group will be action method name
server.addController("/comment/(?<action>\\w+)", ExampleCommentController.class);
server.serv();
} catch (IOException e) {
e.printStackTrace();
}
}
}
对于 router
和 regeRouter
,不要使用类成员变量来传递参数,这会导致多线程竞争的发生,
(因为所有命中该router的请求实际是由同一个实例去处理的)
当然,如果你使用java8,那么更好的方式是使用lambda,这样就不需要考虑这个问题。
router
和 regexRouter
被设计为处理一些非常简单的请求,因此才会使用lambda或者匿名内部类实例,
因为你不会想把所有这些简单的router都作为一个类,单独写到一个java文件里。如果涉及到复杂的业务逻辑,
更好的选择是使用 controller
而不是 router
.
对于 controller
, 每一个命中的请求都会使用一个新的实例去处理。因此你可以放心的使用类成员,这不会造成竞争。
感谢以下成员的为JHttp所作的贡献。 : )
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型