@Router("/book")
public class BookRouter {
// 视图
@Mapping("/")
public ViewResult index() {
return Result.view("/book.html");
}
// json
@Mapping(value = "/ajax", method = {HttpMethod.POST})
public Book find(RoutingContext ctx) {
// ...
return new Book();
}
}
获取请求参数
表单参数或json请求参数
项目将json请求参数与表单参数合并,使用相同的方法或注解获取
使用RoutingContext获取
Router.router().path("/").handler(ctx -> {
String id = ctx.request().getParam("id");
ctx.response().write(id);
});
使用注解获取
@Mapping(value = "/ajax", method = {HttpMethod.POST})
public Book find(@Param Long id, @Param("tp") String type) {
// ...
return new Book();
}
restful参数
使用RoutingContext获取
Router.router().path("/{id}").handler(ctx -> {
String id = ctx.request().getPathVariable("id");
ctx.response().write(id);
});
使用注解获取
@Mapping(value = "/{id}", method = {HttpMethod.POST})
public void ajax(@PathParam("id") Long id) {
// ...
}
header参数
使用RoutingContext获取
Router.router().path("/").handler(ctx -> {
String id = ctx.request().getHeader("id");
ctx.response().write(id);
});
使用注解获取
@Mapping(value = "/", method = {HttpMethod.POST})
public void ajax(@HeaderParam("id") Long id) {
// ...
}
cookie参数
使用RoutingContext获取
Router.router().path("/").handler(ctx -> {
String id = ctx.request().getCookie("id");
ctx.response().write(id);
});
使用注解获取
@Mapping(value = "/", method = {HttpMethod.POST})
public void ajax(@CookieParam("id") Long id) {
// ...
}
参数转对象
实体类
@Data
public class Book {
private String name;
private String author;
}
使用RoutingContext转换
Router.router().path("/").handler(ctx -> {
// 表单或json请求参数绑定
Book book = ctx.bindParam(Book.class);
// cookie参数绑定
book = ctx.bindCookie(Book.class);
// header参数绑定
book = ctx.bindHeader(Book.class);
// restful参数绑定
book = ctx.bindPathVariables(Book.class);
});
使用注解获取
@Mapping(value = "/", method = {HttpMethod.POST})
public void ajax(@Param Book b1, @CookieParam Book b2, @HeaderParam Book b3, @PathParam Book b4) {
// ...
}
@Mapping("/")
public void index(RoutingContext ctx) {
ctx.response().setHeader("hello", "world");
}
拦截器
WebHook是拦截器接口,可以实现执行前、执行后和结束拦截处理
@Slf4j
@Bean
public class LogWebHook implements WebHook {
@Override
public boolean before(RoutingContext ctx) {
log.info("before");
return true;
}
@Override
public void after(RoutingContext ctx) {
log.info("after");
}
@Override
public void finished(RoutingContext ctx) {
log.info("finished");
}
}
异常处理
框架默认提供了一个异常处理器,如需自定义处理异常,可以像下面这样使用
@Bean
public class CustomExceptionHandler extends ExceptionHandlerImpl {
@Override
public void handle(RoutingContext ctx, Exception e, int status) {
if (e instanceof CustomException) {
// do something
} else {
super.handle(ctx, e, status);
}
}
}
部署项目
修改端口
编码指定
Server.server().listen(8080);
配置文件
server.port=8081
运行项目
使用内嵌容器启动
启动类
public class Application {
public static void main(String[] args) {
Server.server().listen();
}
}
JOxygen 2.0 发布,支持函数式编程、web 编程更加简便
虽然不提倡重复造轮子,但好处之一是轮子可以造成方的
2.0全新升级重构版,让web编程更简单
public static void main(String[] args) { Router.router().path("/").handler(ctx -> ctx.response().write("hello world")); Server.server().listen(8080); }用浏览器打开 http://localhost:8080 就可以看到
hello world了!注册路由硬编码方式注解方式获取请求参数表单参数或json请求参数restful参数header参数cookie参数参数转对象静态资源上传文件结果渲染渲染json渲染文本渲染html渲染模板重定向写入cookie添加header拦截器异常处理部署项目修改端口运行项目注册路由
硬编码方式
Router.router().path("/").handler(ctx -> ctx.response().write("hello world")); Router.router().path("/get").method(HttpMethod.GET).handler(get); Router.router().path("/post").method(HttpMethod.POST).handler(post);注解方式
@Router("/book") public class BookRouter { // 视图 @Mapping("/") public ViewResult index() { return Result.view("/book.html"); } // json @Mapping(value = "/ajax", method = {HttpMethod.POST}) public Book find(RoutingContext ctx) { // ... return new Book(); } }获取请求参数
表单参数或json请求参数
项目将json请求参数与表单参数合并,使用相同的方法或注解获取
使用RoutingContext获取
Router.router().path("/").handler(ctx -> { String id = ctx.request().getParam("id"); ctx.response().write(id); });使用注解获取
@Mapping(value = "/ajax", method = {HttpMethod.POST}) public Book find(@Param Long id, @Param("tp") String type) { // ... return new Book(); }restful参数
使用RoutingContext获取
Router.router().path("/{id}").handler(ctx -> { String id = ctx.request().getPathVariable("id"); ctx.response().write(id); });使用注解获取
@Mapping(value = "/{id}", method = {HttpMethod.POST}) public void ajax(@PathParam("id") Long id) { // ... }header参数
使用RoutingContext获取
Router.router().path("/").handler(ctx -> { String id = ctx.request().getHeader("id"); ctx.response().write(id); });使用注解获取
@Mapping(value = "/", method = {HttpMethod.POST}) public void ajax(@HeaderParam("id") Long id) { // ... }cookie参数
使用RoutingContext获取
Router.router().path("/").handler(ctx -> { String id = ctx.request().getCookie("id"); ctx.response().write(id); });使用注解获取
@Mapping(value = "/", method = {HttpMethod.POST}) public void ajax(@CookieParam("id") Long id) { // ... }参数转对象
实体类
@Data public class Book { private String name; private String author; }使用RoutingContext转换
Router.router().path("/").handler(ctx -> { // 表单或json请求参数绑定 Book book = ctx.bindParam(Book.class); // cookie参数绑定 book = ctx.bindCookie(Book.class); // header参数绑定 book = ctx.bindHeader(Book.class); // restful参数绑定 book = ctx.bindPathVariables(Book.class); });使用注解获取
@Mapping(value = "/", method = {HttpMethod.POST}) public void ajax(@Param Book b1, @CookieParam Book b2, @HeaderParam Book b3, @PathParam Book b4) { // ... }静态资源
内置默认将
classpath下/public,/static作为静态资源目录,支持webjars,映射到/public自定义静态资源可使用下面代码
Router.staticRoute().prefix("/lib").location("classpath:lib");也可以通过配置文件指定
上传文件
使用RoutingContext获取
Router.router().path("/").handler(ctx -> { MultipartItem file = ctx.request().getMultipartItem("file"); // ... });使用注解获取
@Mapping("/") public void upload(MultipartItem image, @MultipartParam("file1") MultipartItem file) { // 不使用注解则使用方法参数名作为请求参数名称 // 使用注解指定请求参数名称 }结果渲染
渲染json
// 使用RoutingContext返回 Router.router().path("/").handler(ctx -> { ctx.response().json(new Book("Java", "xxx")); }); // 注解式 @Mapping("/") public Book find() { // 直接返回对象,框架默认处理成json return new Book("Java", "xxx"); }渲染文本
// 使用RoutingContext返回 Router.router().path("/").handler(ctx -> { ctx.response().text("hello world"); });渲染html
// 使用RoutingContext返回 Router.router().path("/").handler(ctx -> { ctx.response().html("<html><body><span>hello world</span></body></html>"); });渲染模板
内置支持了
jsp和thymeleaf模板,默认对应resources下的WEB-INF和templates目录模板使用
// 使用RoutingContext Router.router().path("/").handler(ctx -> { ctx.response().template("index.html"); }); Router.router().path("/").handler(ctx -> { Map<String, Object> attrs = new HashMap<>(); // ... ctx.response().template("index.html", attrs); }); // 注解式 @Mapping("/") public Result index() { return Result.view("index.html"); } @Mapping("/") public Result index() { Map<String, Object> attrs = new HashMap<>(); // ... return Result.view("index.html").addAttributes(attrs); }重定向
Router.router().path("/").handler(ctx -> { ctx.response().redirect("https://github.com/justlive1"); }); @Mapping("/a") public Result index() { // 内部地址 相对于根目录: /b // return Result.redirect("/b"); // 内部地址 相对于当前路径: /a/b // return Result.redirect("b"); // 协议地址 return Result.redirect("https://github.com/justlive1"); }写入cookie
@Mapping("/") public void index(RoutingContext ctx) { ctx.response().setCookie("hello", "world"); ctx.response().setCookie("java", "script", 100); ctx.response().setCookie("uid", "xxx", ".justlive.vip", "/", 3600, true); }添加header
@Mapping("/") public void index(RoutingContext ctx) { ctx.response().setHeader("hello", "world"); }拦截器
WebHook是拦截器接口,可以实现执行前、执行后和结束拦截处理@Slf4j @Bean public class LogWebHook implements WebHook { @Override public boolean before(RoutingContext ctx) { log.info("before"); return true; } @Override public void after(RoutingContext ctx) { log.info("after"); } @Override public void finished(RoutingContext ctx) { log.info("finished"); } }异常处理
框架默认提供了一个异常处理器,如需自定义处理异常,可以像下面这样使用
@Bean public class CustomExceptionHandler extends ExceptionHandlerImpl { @Override public void handle(RoutingContext ctx, Exception e, int status) { if (e instanceof CustomException) { // do something } else { super.handle(ctx, e, status); } } }部署项目
修改端口
编码指定
配置文件
运行项目
使用内嵌容器启动
启动类
public class Application { public static void main(String[] args) { Server.server().listen(); } }通用打包方式
${mainClass}为上面的启动类lib目录存放依赖jar<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>${mainClass}</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>打成
fat-jar:<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.8.RELEASE</version> <executions> <execution> <phase>package</phase> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>使用外部容器(jetty、tomcat等)
无需web.xml配置,打包成
war放入容器即可,实现机制可查看WebContainerInitializer外部化配置
框架可以通过使用配置文件进行修改默认属性