2

我正在尝试使用 Dropwizard 和 Gradle 作为构建系统创建一个简单的微服务。没有数据库,只有 REST 端点要公开。

所以我有一个控制器:

@Path("/domainurl/")
@Produces(MediaType.APPLICATION_JSON)
public class SimpleController {

    @GET
    public Example resourceExample() {
        return new Example("something");
    }
}

我的应用程序主类:

public class Application extends Application<MyConfiguration> {

    @Override
    public void run(MyConfiguration configuration, Environment environment) throws Exception {
        final SimpleCOntroller controller = new SimpleController();
        environment.jersey().register(controller);
    }

    public static void main(String[] args) throws Exception {
        new Application().run(args);
    }
}

示例是一个带有一个字符串属性的简单值对象,此时 MyConfiguration 是一个空类。

和 build.gradle:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.0'
    }
}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

mainClassName = "com.example.Application"

//
dependencies
//

run {
    args 'server', './src/config/microservice.yml'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.1'
}

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
}

但是在构建之后,当我输入:

java -jar MyApp.jar

我仍然得到:

Error: Could not find or load main class com.example.Application

有任何想法吗?

4

2 回答 2

2

使用com.github.johnrengelman.shadow插件时,您需要执行任务并运行在nameshadowJar下创建的工件。<project_root>/build/libs<name>-all.jar

顺便说一句:我知道您只提供了部分代码,但我在这里发现了一些错误(例如SimpleCOntrollerSimpleController)。在这里可以找到关于 dropwizard 入门的精彩教程。

于 2015-02-18T07:56:37.267 回答
2

我只是有同样的错误。请注意您正在将“server your-file-name.yml”添加到程序参数而不是VM选项;-)

于 2016-02-09T19:40:19.893 回答