2015-09-05 180 views
3

我想在我的澤西島2 webapp的啓動時初始化StanfordCoreNLP。 我發現,該ServletContextListener是做到這一點,但我沒有在澤西2右邊的ServletContextListener?澤西島2替代ServletContextListener

所以,我怎麼能在我的球衣2 web應用啓動時加載此代碼:

Properties props = new Properties(); 
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); 
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props); 

編輯

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!-- This web.xml file is not required when using Servlet 3.0 container, 
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <servlet> 
     <servlet-name>com.crawler.c_api.rest.ApplicationResource</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>jersey.config.server.provider.packages</param-name> 
      <param-value>com.crawler.c_api</param-value> 
     </init-param>   
     <init-param> 
      <param-name>jersey.config.server.provider.classnames</param-name> 
      <param-value>com.crawler.c_api.provider.ResponseCorsFilter;org.glassfish.jersey.filter.LoggingFilter</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>com.crawler.c_api.rest.ApplicationResource</servlet-name> 
     <url-pattern>/api/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

的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>2.5.1</version> 
       <inherited>true</inherited> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</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-core</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.9.1</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sourceforge.nekohtml</groupId> 
      <artifactId>nekohtml</artifactId> 
      <version>1.9.13</version> 
     </dependency> 



     <dependency> 
      <groupId>edu.stanford.nlp</groupId> 
      <artifactId>stanford-corenlp</artifactId> 
      <version>3.5.2</version> 
     </dependency> 

     <dependency> 
      <groupId>edu.stanford.nlp</groupId> 
      <artifactId>stanford-corenlp</artifactId> 
      <version>3.5.2</version> 
      <classifier>models</classifier> 
     </dependency> 
    </dependencies> 
    <properties> 
     <jersey.version>2.19</jersey.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
</project> 

ApplicationResource

package com.crawler.c_api.rest; 

import com.crawler.c_api.provider.ResponseCorsFilter; 
import java.util.logging.Logger; 


import org.glassfish.jersey.filter.LoggingFilter; 
import org.glassfish.jersey.server.ResourceConfig; 
import org.glassfish.jersey.server.ServerProperties; 



public class ApplicationResource extends ResourceConfig { 

    private static final Logger LOGGER = null; 


    public ApplicationResource() { 
      System.out.println("iansdiansdasdasd"); 
     // Register resources and providers using package-scanning. 
     packages("main.java.com.crawler.c_api"); 

     // Register my custom provider - not needed if it's in my.package. 
     register(ResponseCorsFilter.class); 

     // Register an instance of LoggingFilter. 
     register(new LoggingFilter(LOGGER, true)); 

     // Enable Tracing support. 
     property(ServerProperties.TRACING, "ALL"); 
    } 
} 

回答

4

ServletContextListener如果servlet API的一部分。所以你可以只加

<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>javax.servlet-api</artifactId> 
    <version>3.1.0</version> 
    <scope>provided</scope> 
</dependency> 

但它可能沒有必要。除了web.xml之外,還有更多的方式來部署Jersey應用程序。您可以擴展ResourceConfig。即使您想要,這也可以讓您完全無xml。您在web.xml中配置的所有內容大部分都可以在ResourceConfig中進行配置。

您甚至可以同時使用web.xml a ResourceConfig。例如

public class MyApplication extends ResourceConfig { 
    public MyApplication() { 
     packages("org.foo.rest;org.bar.rest"); 
     // do any other initialization here 
    } 
} 

<web-app> 
    <servlet> 
     <servlet-name>org.foo.rest.MyApplication</servlet-name> 
    </servlet> 
    ... 
    <servlet-mapping> 
     <servlet-name>org.foo.rest.MyApplication</servlet-name> 
     <url-pattern>/resources</url-pattern> 
    </servlet-mapping> 
    ... 
</web-app> 

查看其他部署選項here。有幾種不同的方式來部署Jersey應用程序。

+0

感謝您的提示。我是否還需要做其他事情才能使ResourceConfig得到執行? –

+0

在該示例中,它在web.xml中被聲明爲servlet-name,而不是通常使用'ServletContainer'作爲servlet-class。另外不要忘了查看更多的例子和解釋的鏈接。 –

+0

我想你還需要確保擁有'jersey-container-sevlet'依賴。 –