无法正确运行DropWizard服务器端
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法正确运行DropWizard服务器端相关的知识,希望对你有一定的参考价值。
我正在开发一个需要RESTful服务器端的android和ios应用程序。直到今天我在日食时使用了带RunJettyRun的Jersey,但我决定开始使用新的更好的东西!我转移到DropWizard和IntelliJ IDEA
这些是我安装的软件和我已经处理过的东西: - Java 8 - Maven 3.1.1 - Maven和Java的环境变量。
那么我现在所做的是在他们的网站上遵循DropWizard入门教程。我完全按照他们的说法完成,最后我尝试运行我从IntelliJ终端中的mvn package命令获得的jar。结果是在输出中:
usage: java -jar dropwizard-1.0-SNAPSHOT.jar
[-h] [-v] {server,check} ...
positional arguments:
{server,check} available commands
optional arguments:
-h, --help show this help message and exit
-v, --version show the application version and exit
Process finished with exit code 0
代码 :
资源类别:
@Path("/hello-world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloResource {
private final String template;
private final String defaultName;
private final AtomicLong counter;
public HelloResource(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);
}
}
配置类:import com.fasterxml.jackson.annotation.JsonProperty; import io.dropwizard.Configuration; import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.Valid;
/**
* Created by Ido on 2/25/2015.
*/
public class Conf 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;
}
}
代表类:公共类说{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 io.dropwizard.Application;
import io.dropwizard.java8.Java8Bundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
/**
* Created by Ido on 2/25/2015.
*/
public class ExampleService extends Application<Conf> {
public static void main(String [] args) throws Exception {
new ExampleService().run(args);
}
@Override
public String getName() {
return "Hello World";
}
@Override
public void initialize(Bootstrap<Conf> bootstrap) {
bootstrap.addBundle(new Java8Bundle());
}
@Override
public void run(Conf conf, Environment environment) throws Exception {
final HelloResource resource = new HelloResource(
conf.getTemplate(),
conf.getDefaultName()
);
final TemplateHealthCheck healthCheck =
new TemplateHealthCheck(conf.getTemplate());
environment.healthChecks().register("template", healthCheck);
environment.
jersey().register(resource);
}
}
POM文件
<modelVersion>4.0.0</modelVersion>
<groupId>ido.dropwizard.example</groupId>
<artifactId>dropwizard</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<dropwizard.version>0.7.1</dropwizard.version>
</properties>
<dependencies>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-java8</artifactId>
<version>0.7.0-1</version>
</dependency>
<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-java8-auth</artifactId>
<version>0.7.0-1</version>
</dependency>
<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-java8-jdbi</artifactId>
<version>0.7.0-1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<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>ido.dropwizard.example.ExampleService</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
我添加了整个代码,因为我真的需要一个答案,因为我在youtube和google上的其他一些教程中遇到了这个问题,无法让它工作......顺便说一句,当我试图测试这个我在浏览器上输入时 - http://localhost:8080/hello-world/something还有http://localhost:8080/hello-world =>没工作:)
好吧,我将非常感谢任何帮助,谢谢:)
Dropwizard接受第一个命令行参数并将其分派给匹配的命令。在这种情况下,可用的命令是server和check,它将您的应用程序作为HTTP服务器运行。 server命令需要配置文件,你可以这样做:
java -jar target/dropwizard-1.0-SNAPSHOT.jar server config.yml
如果它在子文件夹中
java -jar target/dropwizard-1.0-SNAPSHOT.jar server conf/config.yml
如果你没有config.yml,请创建一个这样的模板:
以上是关于无法正确运行DropWizard服务器端的主要内容,如果未能解决你的问题,请参考以下文章
使用片段时 Intellij 无法正确识别 Thymeleaf 模型变量