修改上一篇文章中主程序,实现http请求的路由功能。
- 在pom.xml中增加新的依赖
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
- 在主程序中增加两个访问入口
public class MyFirstRXVerticle extends AbstractVerticle {
@Override
public void start() {
Router router = Router.router(vertx);
router.get("/").handler(rc -> rc.response().end("hello"));
router.get("/:name").handler(rc -> rc.response()
.end("hello " + rc.pathParam("name")));
vertx.createHttpServer()
.requestHandler(router::accept)
.listen(8080);
}
}
- eclipse中根据提示引入新的包
import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
- 重新选择Run->maven install
- Run->java application
- 运行结果:
在浏览器中访问:
localhost:8080 返回结果:hello
localhost:8080/name 返回结果:hello name
localhost:8080/liuliuliu 返回结果:hello liuliuliu