Incompatible types, equality constraints and method not found during Java 9 Migration

后端 未结 1 1919
终归单人心
终归单人心 2020-12-15 22:45

While migrating one of our projects to Java 9(build 9+181), I am facing a peculiar problem what looks like an incorrect implementation in some libr

相关标签:
1条回答
  • 2020-12-15 23:10

    From the sample project I was able to fix the compilation issue. There were 2 exceptions in com.SomeService#run method. There were missing modules in your module-info.java, once you add these the code should compile.

    requires dropwizard.jersey;
    requires dropwizard.jetty;
    

    JerseyEnvironment comes from io.dropwizard:dropwizard-jersey:1.1.0

    ServletEnvironment comes from io.dropwizard:dropwizard-jetty:1.1.0

    Since they are different jars, they export different modules. Hence, the requires needs to be explicitly added. Your code works fine without module-info.java because at that time module system is not used.

    I found the fix by doing the below, method mentioned in comments:

    @Override
    public void run(SomeServiceConfig config, Environment environment) throws Exception {
        Injector injector = Guice.createInjector(new SomeServiceModule());
        // Fix: Extract to variable to find Type of jersey and then find the module to add under requires
        JerseyEnvironment jersey = environment.jersey(); 
        jersey.register(injector.getInstance(SomeResource.class));
        // Fix 2: Same method as Fix 1
        ServletEnvironment servlets = environment.servlets();
        servlets.addFilter("Some-Filter", SomeFilter.class);
    }
    
    0 讨论(0)
提交回复
热议问题