企业即时通信软件-杭州钛特云

您确定要退出登录吗?

修改密码

原密码
新密码
确认新密码
客服微信
产品咨询

tio-http-server

talent-tan


框架/工具 t-io tio
案例 企业即时通讯 tio-http-server

产品介绍

用t-io实现http协议,并且内置了mvc能力,使用非常方便

使用示范

一. 启动代码

代码是实用型的代码,并不是为demo而demo,所以代码行数看着多一些(许多其它框架为了展示自己的极简,把许多东西全部封在一行代码里,美其名曰:“一行代码启动”,实际上这并不实用)

package org.tio.http.server.showcase.init;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.handler.HttpRequestHandler;
import org.tio.http.server.HttpServerStarter;
import org.tio.http.server.handler.DefaultHttpRequestHandler;
import org.tio.http.server.showcase.HttpServerShowcaseStarter;
import org.tio.server.ServerTioConfig;
import org.tio.utils.jfinal.P;
/**
 * @author tanyaowu
 * 2017年7月19日 下午4:59:04
 */
public class HttpServerInit {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(HttpServerInit.class);
	public static HttpConfig httpConfig;
	public static HttpRequestHandler requestHandler;
	public static HttpServerStarter httpServerStarter;
	public static ServerTioConfig serverTioConfig;
	public static void init() throws Exception {
		int port = P.getInt("http.port");//启动端口
		String pageRoot = P.get("http.page");//html/css/js等的根目录,支持classpath:,也支持绝对路径
		httpConfig = new HttpConfig(port, null, null, null);
		httpConfig.setPageRoot(pageRoot);
		httpConfig.setMaxLiveTimeOfStaticRes(P.getInt("http.maxLiveTimeOfStaticRes"));
		httpConfig.setPage404(P.get("http.404"));
		httpConfig.setPage500(P.get("http.500"));
		httpConfig.setUseSession(false);
		httpConfig.setCheckHost(false);
		requestHandler = new DefaultHttpRequestHandler(httpConfig, HttpServerShowcaseStarter.class);//第二个参数也可以是数组
		httpServerStarter = new HttpServerStarter(httpConfig, requestHandler);
		serverTioConfig = httpServerStarter.getServerTioConfig();
		httpServerStarter.start(); //启动http服务器
	}
}


package org.tio.http.server.showcase;
import org.tio.http.server.showcase.init.HttpServerInit;
import org.tio.utils.jfinal.P;
/**
 * @author tanyaowu
 * 2017年6月28日 下午5:34:04
 */
public class HttpServerShowcaseStarter {
	/**
	 * @param args
	 * @author tanyaowu
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		P.use("app.properties");
		HttpServerInit.init();
	}
}

二. Controller控制器代码

@RequestPath(value =  "/test")
public class TestController {	
	@RequestPath(value = "/testStr")
	public String testStr(HttpRequest request, String test) throws Exception {
		return test;
	}

	@RequestPath(value = "/var/{test}")
	public String var(HttpRequest request, String test) throws Exception {
		return test;
	}

	@RequestPath(value = "/abtest")
	public HttpResponse abtest2(HttpRequest request) throws Exception {
		log.info("");
		HttpResponse ret = Resps.html(request, "OK");
		return ret;
	}
}



登录 和大家一起讨论吧!

重置 发表评论


确定要删除该条评论吗?

企业即时通讯 案例不存在!