本人第一篇博客,简单介绍一下Dropwizard框架的搭建,多多指教
本人都是从官方网站上学习到的,游客们可以参考以下连接学习:dropwizard Get-Start
下面开始一步一步的搭建环境:(Intellij14.1, JDK1.7)
1.1 添加依赖包
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.8.5</version>
</dependency>
后面统一都用0.8.5这个版本,用Intellij 工具maven可以自动导入这些依赖包
1.2 添加yml文件
笔者认为这个文件类似于普通web项目中的 .xml文件,对一些的项目配置之类的,可以去https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example git-hub上的实例下载yml文件
以后会具体分析yml文件的各个关键字的具体用处,文件中很多东西是多出来的,必须要删除或者注释掉才能正常启动项目,此博客只简单介绍helloworld的项目(所有都是helloworld为第一个项目)
1.3 添加example.keystore 文件
同样的在上面那个地址去找,笔者只知道没有这个文件不能运行整个项目,具体作用不太清楚
2.1 写 Configuration 配置类
import io.dropwizard.Configuration;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
public class HelloWorldConfiguration extends Configuration {
@NotEmpty
private String template;
@NotEmpty
private String defaultName = "Stranger";
@JsonProperty
public String getTemplate() {
return template;
}
@JsonProperty
public void setTemplate(String template) {
this.template = template;
}
@JsonProperty
public String getDefaultName() {
return defaultName;
}
@JsonProperty
public void setDefaultName(String name) {
this.defaultName = name;
}
}
先简单的搭建起来,让项目能跑起来,具体的意义后面分析,包括其中的一些Annotation,有框架基础的一眼或许就能看懂,此处的template 和 defaultname 可以不要,但是和yml文件中的关键字是有关联的,如果要删除的话一定要将yml文件中的template 、defaultName一起删除,而且在后面的resource类中一个方法的参数也要改变(初学者建议就按照这个来,先让程序跑起来,然后在进行改动)
2.2 写应用Application 类
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import com.example.helloworld.resources.HelloWorldResource;
public class HelloWorldApplication extends Application<HelloWorldConfiguration> {
public static void main(String[] args) throws Exception {
new HelloWorldApplication().run(args);
}
@Override
public String getName() {
return "hello-world";//在项目启动之后,负责URL跳转
}
@Override
public void initialize(Bootstrap<HelloWorldConfiguration> bootstrap) {
}
@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
}
}
此类与Jersey 服务器有关,建立该项目的时候若想运行需要“注册”(笔者自己的理解)环境和配置,也就是与上文的配置类息息相关此时我们只是将关键代码展示,后面进行补充,一步一步say hello world
大部分的注册信息都放在 run方法中,涉及到数据库、页面层的一些初始化配置和资源配置,需要在initialize方法中写,以后的文章中会具体再写
2.3 写入API类
(此处其实不是必要的,但是上面两个继承了Configuration 和 Application 的两个类是必须要有的,本文先按照官网的example来写)
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;
public class Saying {
private long id;
@Length(max = 3)
private String content;
public Saying() {
// Jackson deserialization
}
public Saying(long id, String content) {
this.id = id;
this.content = content;
}
@JsonProperty
public long getId() {
return id;
}
@JsonProperty
public String getContent() {
return content;
}
}
import com.example.helloworld.api.Saying;
import com.google.common.base.Optional;
import com.codahale.metrics.annotation.Timed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.concurrent.atomic.AtomicLong;
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
public HelloWorldResource(String template, String defaultName) {
this.template = template;
this.defaultName = defaultName;
this.counter = new AtomicLong();
}
@GET
@Timed
public Saying sayHello(@QueryParam("name") Optional<String> name) {
final String value = String.format(template, name.or(defaultName));
return new Saying(counter.incrementAndGet(), value);
}
}
此类 相当于其他框架中的Controller 、Action
其中的Annotation 先简单的解释一下,以后有机会详细介绍,因为我们这个项目中暂时没有视图层(dropwizard 到目前笔者了解到不支持jsp,只能支持freemarker和mustache作为视图层解析),所以“hello-world” 路径返回的是一个Json格式的数据而不是HTML格式的
@Path 为该项目的一个URL 可以直接跳转进入到此类中的方法,然后在后台进行处理
@Produces 为该类(或者方法)返回的数据类型,默认为Json格式
@Get 限制客户端访问时只能通过get方法访问
@QueryParam 为URL链接后面的参数(url ?{参数名} = {参数值} 的格式)
此处的Optional 类具体用法自行百度、谷歌,笔者也是初学者解释不清,此处的用法就是当参数(name)为空的时候调用defaultName
3.1 在run方法添加资源
在HelloWorldApplication 类中注册Resource资源,这就是上文讲过的,所有资源都必须到Application类中注册
在run方法中添加如下代码
@Override
public void run(HelloWorldConfiguration configuration,
Environment environment) {
final HelloWorldResource resource = new HelloWorldResource(
configuration.getTemplate(),
configuration.getDefaultName()
);
environment.jersey().register(resource);//environment环境类添加到Jersey服务器中
}
官网中有Template 模板检验,并不是必须的,笔者将这一块都略去了
添加如下代码到 pom文件中
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.helloworld.HelloWorldApplication</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
将该文件更改之后,整个项目基本就算完成了,我们尝试一下运行
直接到Application 类中运行主线程 main方法
3.3 运行环境更改(配置)
首先得用maven打包项目为jar包
可以用开发工具快速打包
其实这一块,笔者也不太懂原理,有懂的可以留言交流
打包之后运行主程序
可能会出现上图的问题,我们在运行配置中更改一下参数就可以了(当然也可以直接用命令行启动程序时,加入参数,像官网上 java -jar target/****-1.0-SNAPSHOT.jar server ****.yml) 其中的 * 号是与你自己命名的
有的同学可能不知道Intellij 程序参数怎么更改,截图教大家
搞定之后就可以运行了
4.1 可能出现的问题
yml 文件中多余的参数,如果你按照此文章来一步一步做的话,yml 文件中参数肯定有多余的,因为我们并没有配置数据库的资源类,也没有template的校验等,但是下载的yml文件却是完整的包含这些内容的,所以出现一些多余参数只需要注释掉,或者删除掉,以后用到的时候再加上。
yml文件中的参数的格式不同于xml 和 properties文件,而且格式要求比较高,有时候多一个空格就会报错(参数的开头处多个空格,总之格式挺严格的)
出现的其他问题,一般都是配置或者路径问题了
运行成功之后再localhost:8080/hello-world (localhost:8080/hello-world?name=*****)可以看到结果
整个项目的结构如下图