Я использую Guice в Jersey2 для DI (я хочу использовать его, чтобы использовать Google App Engine -> не работая с HK2).Guice nullpointer exception in instanceed instance
Мой ApplicationResource:
public class ApplicationResource extends ResourceConfig {
private static final Logger LOGGER = null;
public ApplicationResource() {
System.out.println("Application startup");
// Register resources and providers using package-scanning.
packages("com.crawler.c_api");
// Register my custom provider - not needed if it's in my.package.
register(ResponseCorsFilter.class);
Injector createInjector = Guice.createInjector(new AbstractModule(){
@Override
protected void configure() {
bind(NLPProvider.class).toInstance(new NLPProvider());
}
});
// Register an instance of LoggingFilter.
register(new LoggingFilter(LOGGER, true));
// Enable Tracing support.
property(ServerProperties.TRACING, "ALL");
EncodingFilter.enableFor(this, GZipEncoder.class);
}
}
NLPProvider загружает кучу файлов - и файлы загружены правильно. Но когда я пытаюсь ввести экземпляр NLPProvider, он остается равным нулю.
CrwalerResource:
@Path("/crawler")
public class CrawlerResource {
@Inject
NLPProvider pipeline;
@GET
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public Response crawlUrl(@BeanParam CrawlerQueryParam queryParameters) {
CrawlerService crawl = new CrawlerService();
return Response.status(200).entity(crawl.extractFromUrl(queryParameters, pipeline)).build();
}
}
Что я делаю неправильно, учитывая, что я использовал HK2 раньше?
Мой pom.xml - может быть, вопрос идет отсюда:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.crawler</groupId>
<artifactId>C_API</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>C_API</name>
<build>
<finalName>C_API</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.3m</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.syncthemall</groupId>
<artifactId>boilerpipe</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.apache.opennlp</groupId>
<artifactId>opennlp-tools</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>14.0.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.github.mpkorstanje</groupId>
<artifactId>simmetrics</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.cybozu.labs</groupId>
<artifactId>langdetect</artifactId>
<version>1.1-20120112</version>
</dependency>
<dependency>
<!-- jsoup HTML parser library @ http://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.21</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Спасибо - что решить мою проблему для Guice. Но поскольку я все еще как-то использую HK2, он не работает с движком Google App. Я знаю, что это будет другой вопрос, но все же - любые предложения для этого? –
Я не знаю, я никогда не использовал GAE, поэтому я не знаю, как это работает? Что насчет Весны? Не совсем уверен, как/если вы можете использовать Jersey 2 и Guice без участия Hk2. Я знаю Джерси 1, ты можешь. –
Да, это сложно. Теперь я посмотрю на эластичный бобовый стебель. Спасибо за помощь :) –