vertx打成jar包发布工程,访问静态页面

时间:2021-08-11 15:10:46

1:添加pom依赖,配置打包插件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.li</groupId>
<artifactId>vertxJarPackTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <dependencies>
<!-- vertx -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.5.0</version>
</dependency>
<!-- 动态模板引擎,thymeleaf引擎 -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-thymeleaf</artifactId>
<version>3.5.0</version>
</dependency>
<!-- 动态模板引擎 ,handlebars引擎-->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-handlebars</artifactId>
<version>3.5.0</version>
</dependency> <!-- vertx-JDBC -->
<!-- <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-jdbc-client</artifactId>
<version>3.2.1</version>
</dependency> -->
<!-- vertx-mysql-postgresql-client -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-postgresql-client</artifactId>
<version>3.5.0</version>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>slf4j-impl</artifactId>
<version>RELEASE</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Starter</Main-Class>
<Main-Verticle>staticResourceTest.StaticResourceTest</Main-Verticle><!--
配置自己的主Verticle -->
</manifestEntries>
</transformer>
</transformers>
<artifactSet />
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2:创建工程,创建verticle事件类,start里面是将某一个访问路径路由到静态页面功能

package staticResourceTest;

import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.TemplateHandler;
import io.vertx.ext.web.templ.HandlebarsTemplateEngine;
import io.vertx.ext.web.templ.TemplateEngine;
import io.vertx.ext.web.templ.ThymeleafTemplateEngine; public class StaticResourceTest extends AbstractVerticle{ @SuppressWarnings("deprecation")
public void start(){
Router router=Router.router(vertx);//vertx是AbstractVerticle中的属性
router.route().handler(BodyHandler.create()); //处理请求体body,设置请求体等 router.route("/html/*").handler(StaticHandler.create("html"));//路由到classpath某一个目录下的静态页面
// ThymeleafTemplateEngine engine = ThymeleafTemplateEngine.create();
// ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
// resolver.setPrefix("templates");
// resolver.setSuffix(".html");
// resolver.setTemplateMode("HTML5");
// engine.getThymeleafTemplateEngine().setTemplateResolver(resolver);
//
// // 配置动态文件访问路径
// router.route("/hello").handler(routingContext -> {
// routingContext.put("msg", "DynamicReference.");
// engine.render(routingContext, "/hello", res -> {
// if (res.succeeded()) {
// routingContext.response().putHeader("Content-Type", "text/html").end(res.result());
// } else {
// routingContext.fail(res.cause());
// }
// });
// });
vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}
}

在src/main/resources 下面建立一个html目录,通过下面的路由,可以找到文件下的静态文件

router.route("/html/*").handler(StaticHandler.create("html"));//路由到classpath某一个目录下的静态页面

vertx打成jar包发布工程,访问静态页面

3:选中工程右键run as>maven clean, maven install .

target里出现 ***-fat.jar

4:将jar文件放到任意位置,通过cmd窗口进入:输入>java -jar vertxJarPackTest-0.0.1-SNAPSHOT-fat.jar

浏览器输入地址即可,访问成功。

vertx打成jar包发布工程,访问静态页面