【问题标题】:Cant run a DropWizard server side properly无法正确运行 DropWizard 服务器端
【发布时间】:2015-02-27 14:40:51
【问题描述】:

我正在开发一个需要 RESTful 服务器端的 android 和 iOS 应用程序。 直到今天我在 Eclipse 上使用带有 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);
    }
}

配置类: 导入 com.fasterxml.jackson.annotation.JsonProperty; 导入 io.dropwizard.Configuration; 导入 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;
    }
}

表示类: 公共课说{ 私人长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/somethinghttp://localhost:8080/hello-world => 没用 :)

我会非常感谢任何帮助,谢谢:)

【问题讨论】:

  • 你运行了什么命令?它表明该应用程序未从您显示的内容运行。当您从命令行看到帮助菜单时,这意味着您的命令不正确
  • 执行此操作,将 yaml 配置文件放在与 jar 相同的目录中,然后将 cd 放入该目录(我不使用 Itellij,所以我不知道如何从他们的控制台执行此操作)。如果您在系统中本地安装了 maven,我只需从命令行转到,并使用 java -jar dropwizard-1.0-SNAPSHOT.jar server config.yml。如果您从一开始就按照 t 的说明进行操作,那应该可以。
  • 更好的是,不要将配置放在 jar 的位置。它将在clean 上被删除。而是将其放在项目根目录中。然后cd到项目根目录(如果你还没有构建,那么mvn clean package),然后java -jar target/dropwizard-1.0-SNAPSHOT.jar server config.yml
  • 如果你没有配置文件(你没有展示),那么你应该再次阅读指南,他们展示了它应该是什么样子,并且会有例子。需要这个配置文件。这就是您的 Conf 类获取值的方式
  • peeskillet 首先非常感谢您,因为当我尝试通过简单地单击它并运行来执行 jar 时,我没有工作,因为我需要提供 {server} {config.yml} 值.. . 总之它工作了! :)

标签: maven intellij-idea jersey jetty dropwizard


【解决方案1】:

Dropwizard 采用第一个命令行参数并将其分派给匹配的命令。在这种情况下,可用的命令是 server 和 check,它将您的应用程序作为 HTTP 服务器运行。服务器命令需要一个配置文件,你可以这样做:

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,创建一个像这样的模板:

config.yml

【讨论】:

    猜你喜欢
    • 2016-08-26
    • 2018-04-04
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多