Dropwizard是一个Java框架,其目标是提供高性能、高可靠的Web 应用程序的实现。
一、使用Maven原型创建项目
1、新建项目->选择Maven Archetype
填写好项目名称、路径、选择JDK版本,这里使用JDK11及以上的版本,如下图所示:
2、添加原型
注意上面标明的原型添加的位置,点击添加按钮,输入原型信息,如下图所示:
这里使用的原型信息如下:
GroupId=io.dropwizard.archetypes
ArtifactId=java-simple
Version=4.0.0
具体使用哪个版本的原型可以参考:
dropwizard/dropwizard-archetypes at master · dropwizard/dropwizard · GitHub
原型添加成功之后,会出现附加的属性信息如下:
3、高级设置后创建项目
设置当前项目的信息后点击创建项目,如下:
项目创建成功之后,对应的项目目录如下:
二、启动项目
这里我们直接运行 trueApplication会打印以下提示信息:
usage: java -jar project.jar [-h] [-v] {server,check} ...
positional arguments:
{server,check} available commands
named arguments:
-h, --help show this help message and exit
-v, --version show the application version and exit
从上面打印出的提示信息来看,运行项目需要使用包才能运行;
这里,我们直接修改pom文件,配置包执行插件:
1、在build -> pluginMangement -> plugins 下添加插件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
</plugin>
插件的版本可以在maven仓库中找到,地址如下:
Maven Repository: org.codehaus.mojo » exec-maven-plugin (mvnrepository.com)
2、在build -> plugins 下添加插件执行的配置
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>org.example.trueApplication</mainClass>
<arguments>
<argument>server</argument>
</arguments>
</configuration>
</plugin>
重新加载maven配置后,会在maven工具中看到exec插件,如下图:
双击exec插件下的exec:java,项目就能正常启动了,项目启动的日志如下:
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< org.example:demo >--------------------------
[INFO] Building true 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ demo ---
WARN [2023-05-20 15:09:32,509] org.hibernate.validator.internal.properties.javabean.JavaBeanExecutable: HV000254: Missing parameter metadata for ResponseMeteredLevel(String, int), which declares implicit or synthetic parameters. Automatic resolution of generic type information for method parameters may yield incorrect results if multiple parameters have the same erasure. To solve this, compile your code with the '-parameters' flag.
INFO [2023-05-20 07:09:32,657] io.dropwizard.core.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO [2023-05-20 07:09:32,659] io.dropwizard.core.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO [2023-05-20 07:09:32,667] io.dropwizard.core.server.ServerFactory: Starting true
================================================================================
true
================================================================================
INFO [2023-05-20 07:09:32,784] org.eclipse.jetty.setuid.SetUIDListener: Opened application@daffda3{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
INFO [2023-05-20 07:09:32,784] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@603b59db{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}
INFO [2023-05-20 07:09:32,787] org.eclipse.jetty.server.Server: jetty-11.0.14; built: 2023-02-22T23:41:48.575Z; git: 4601fe8dd805ce75b69c64466c115a162586641b; jvm 11.0.19+7-LTS
INFO [2023-05-20 07:09:33,444] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
NONE
INFO [2023-05-20 07:09:33,448] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@62902118{/,null,AVAILABLE}
INFO [2023-05-20 07:09:33,452] io.dropwizard.core.setup.AdminEnvironment: tasks =
POST /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
POST /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)
WARN [2023-05-20 07:09:33,452] io.dropwizard.core.setup.AdminEnvironment:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW !
! IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE !
! LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR !
! APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO [2023-05-20 07:09:33,453] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@29f22138{/,null,AVAILABLE}
INFO [2023-05-20 07:09:33,485] org.eclipse.jetty.server.AbstractConnector: Started application@daffda3{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
INFO [2023-05-20 07:09:33,494] org.eclipse.jetty.server.AbstractConnector: Started admin@603b59db{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}
INFO [2023-05-20 07:09:33,496] org.eclipse.jetty.server.Server: Started Server@6115115a{STARTING}[11.0.14,sto=30000] @4841ms
通过日志我们可以看出项目使用Jetty作为服务容器;项目启动后默认监听了两个端口,分别是
8080:对外提供服务的端口
8081:对外提供管理服务的端口
三、访问服务
1、访问8080端口
访问地址: http://localhost:8080/
返回的结果如下:
{
"code": 404,
"message": "HTTP 404 Not Found"
}
原因是,我们没有开发对外访问的服务
2、访问8081端口
访问地址: http://localhost:8081/
返回的结果如下:
原因是8081端口提供的是管理服务