与ratpack-pac4j相关的RatpackServer.start导致非静态上下文错误

时间:2016-05-12 12:34:54

标签: java authentication gradle oauth ratpack

对于RatpackServer的启动方法,我有一点非常难以理解(对我而言)。

Fristly,我的意思是这个方法

static RatpackServer start(Action<? super RatpackServerSpec> definition)
                throws Exception

这是我的代码:

public static void main(String... args) throws Exception {

    ProgramInitializer programInitializer = new ProgramInitializer();
    programInitializer.initialize();


    RatpackServer.start(b -> b
            .serverConfig(s -> s
                    .baseDir(BaseDir.find())
                    .env()
            )
            .registry(
                    Guice.registry(s -> s
                            .module(TextTemplateModule.class, conf ->
                                    conf.setStaticallyCompile(true)
                            )
                            .bindInstance(ServerErrorHandler.class, (ctx, error) -> {
                                logger.error("Unexpected error", error);
                                ctx.render(groovyTemplate("error500.html"));
                            })
                            .bindInstance(ClientErrorHandler.class, (ctx, statusCode) -> {
                                ctx.getResponse().status(statusCode);
                                if (statusCode == 404) {
                                    ctx.render(groovyTemplate("error404.html"));
                                } else if (statusCode == 401) {
                                    ctx.render(groovyTemplate("error401.html"));
                                } else if (statusCode == 403) {
                                    ctx.render(groovyTemplate("error403.html"));
                                } else {
                                    logger.error("Unexpected: {}", statusCode);
                                }
                            })
                    )
            )
            .handlers(c -> { c
                                .get("index.html", ctx -> {
                                    //ctx.redirect(301, "/");
                                    logger.debug("Retrieving user profile...");
                                    final Map<String, Object> model = Maps.newHashMap();
                                    RatpackPac4j.userProfile(ctx)
                                            .route(Optional::isPresent, p -> {
                                                model.put("profile", p);
                                                ctx.render(groovyTemplate(model, "index.html"));
                                            })
                                            .then(p -> {
                                                ctx.render(groovyTemplate(model, "index.html"));
                                            });
                                    //
                                })
                                //.get(ctx -> ctx.render(groovyTemplate("index.html")))

                                .get("hello", ctx -> {
                                    RelativisticModel.select();
                                    //String energy = System.getenv("ENERGY");
                                    //Amount<Mass> m = Amount.valueOf(energy).to(KILOGRAM);
                                    ctx.render(programInitializer.getTweetsList());
                                })

                                .files(f -> f.dir("public"));
                    }
            )
    );
}

一切顺利,直到我将此依赖项添加到build.gradle:

compile 'io.ratpack:ratpack-pac4j:1.3.3'

这样整个文件看起来像这样:

group 'pl.edu.agh.tai.tdo'
version '1.0'

apply plugin: 'java'
apply plugin: 'idea'

buildscript {
    repositories {
        maven { url "http://oss.jfrog.org/repo" }
        mavenCentral()
        maven { url "http://dl.bintray.com/robfletcher/gradle-plugins" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        jcenter()
    }
    dependencies {
        classpath "io.ratpack:ratpack-gradle:1.0.0"
        classpath ('org.ratpack-framework:ratpack-gradle:0.9.0-SNAPSHOT')
    }
}

apply plugin: "io.ratpack.ratpack-groovy"

repositories {
    maven { url "http://oss.jfrog.org/repo" }
    mavenCentral()
    maven { url "http://repo.springsource.org/repo" } // for springloaded
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    jcenter()
}

mainClassName = "pl.edu.agh.tai.tdo.Main"

dependencies {

    compile "org.ratpack-framework:ratpack-handlebars:0.9.0-SNAPSHOT"

    runtime "org.slf4j:slf4j-simple:1.7.12"
    compile "com.heroku.sdk:heroku-jdbc:0.1.1"
    compile "org.postgresql:postgresql:9.4-1201-jdbc4"
    compile "org.jscience:jscience:4.3.1"

    compile 'io.ratpack:ratpack-pac4j:1.2.0'
    compile 'org.twitter4j:twitter4j-core:4.0.4'
    compile 'ch.qos.logback:logback-classic:1.1.7'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

sourceSets {
    main.java.srcDir "src/main/java"
    main.resources.srcDirs "src/main/resources"
    test.java.srcDir "src/test/java"
}

task stage {
    dependsOn installDist
}

在Main类中添加此依赖项

Error:java: incompatible types: com.google.inject.Injector is not a functional interface
multiple non-overriding abstract methods found in interface com.google.inject.Injector


Error:(29, 22) java: method start in interface ratpack.server.RatpackServer cannot be applied to given types; required: no
arguments found: (b)->b.ser[...]); }) 
reason: actual and formal argument lists differ in length


Error:(63, 56) java: invalid method reference

非静态方法isPresent()不能从静态上下文引用

在IntelliJ中的RatpackServer.start(...)上面写了

Non-static method start() cannot be referenced from a static context

有没有这样的问题?这对我来说真的很神秘。我尝试基于Ratpack构建我的第一个应用程序...

1 个答案:

答案 0 :(得分:1)

您似乎正在混合和匹配Ratpack的多个版本。

在您的构建文件中,我找到:

  • Ratpack 0.9.0
  • Ratpack 1.2.0
  • Ratpack 1.0.0

您使用的RatpackServer版本没有start方法作为静态方法。

我已经为您修复了构建gradle文件:

plugins {
  id 'io.ratpack.ratpack-groovy' version '1.3.3' // downloads ratpack-groovy 1.3.3 which is latest as of 2016-05-12
  id 'idea' // new way of applying idea plugin
}

group 'pl.edu.agh.tai.tdo'
version '1.0'

repositories {
    maven { url "http://oss.jfrog.org/repo" }
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    jcenter()
}

mainClassName = "pl.edu.agh.tai.tdo.Main"

dependencies {
    compile ratpack.dependency('handlebars') // use ratpack gradle plugin to ensure that ratpack-handlebars 1.3.3 is used
    compile ratpack.dependency('pac4j') // use ratpack gradle plugin to ensure that ratpack-pac4j 1.3.3 is used

    runtime "org.slf4j:slf4j-simple:1.7.12"
    compile "com.heroku.sdk:heroku-jdbc:0.1.1"
    compile "org.postgresql:postgresql:9.4-1201-jdbc4"
    compile "org.jscience:jscience:4.3.1"

    compile 'org.twitter4j:twitter4j-core:4.0.4'
    compile 'ch.qos.logback:logback-classic:1.1.7'

    testCompile group: 'junit', name: 'junit', version: '4.11'
}

task stage(dependsOn: installDist)

Ratpack Gradle插件,在您调用

时应用
plugins {
  id 'io.ratpack.ratpack-groovy' version '1.3.3'
}

提供方法ratpack.dependency,它将引入给定模块的适当版本,例如pac4j。

以下是等效的:

dependencies {
    compile ratpack.dependency('pac4j')
}

dependencies {
    compile 'io.ratpack:ratpack-pac4j:1.3.3'
}