2014-11-08 69 views
0

我想运行泽西岛REST服务与嵌入式码头服务器。这是我的资源类:泽西岛与嵌入式码头服务器

public class FileUploadService { 

@POST 
@Path("/upload") 
@Consumes(MediaType.MULTIPART_FORM_DATA) 
public Response uploadFile(
     @FormDataParam("file") InputStream uploadedInputStream, 
     @FormDataParam("file") FormDataContentDisposition fileDetail){ 

    String uploadedFileLocation = "/uploaded" + fileDetail.getFileName(); 
    writeToFile(uploadedInputStream, uploadedFileLocation); 
    String output = "File uploaded to: " + uploadedFileLocation; 
    return Response.status(200) 
        .header("Access-Control-Allow-Origin", "http://localhost:63342") 
        .allow("OPTIONS") 
        .build(); 


    } 
} 

从来就还增加了main方法嵌入式服务器:

public class SimpleServer { 

public static void main(String[] args) throws Exception { 
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
    context.setContextPath("/"); 

    Server jettyServer = new Server(4343); 
    jettyServer.setHandler(context); 

    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*"); 
    jerseyServlet.setInitOrder(0); 

    // Tells the Jersey Servlet which REST service/class to load. 
    jerseyServlet.setInitParameter(
     "/", 
     FileUploadService.class.getCanonicalName()); 

    try { 
     jettyServer.start(); 
     jettyServer.join(); 
    } finally { 
     jettyServer.destroy(); 
    } 
} 

} 

从来就添加以下依存关系到POM文件:

<groupId>JAXRS-FileUpload</groupId> 
<artifactId>JAXRS-FileUpload</artifactId> 
<version>1.0-SNAPSHOT</version> 


<properties> 
    <java.source.level>1.7</java.source.level> 
    <java.target.level>1.7</java.target.level> 
</properties> 

<repositories> 
    <repository> 
     <id>maven2-repository.java.net</id> 
     <name>Java.net Repository for Maven</name> 
     <url>http://download.java.net/maven/2/</url> 
     <layout>default</layout> 
    </repository> 
</repositories> 

<dependencies> 
    <dependency> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-server</artifactId> 
     <version>9.3.0.M0</version> 
    </dependency> 

    <dependency> 
     <groupId>org.eclipse.jetty</groupId> 
     <artifactId>jetty-servlet</artifactId> 
     <version>9.3.0.M0</version> 
    </dependency> 

    <dependency> 
     <groupId>org.glassfish.jersey.core</groupId> 
     <artifactId>jersey-server</artifactId> 
     <version>2.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-servlet-core</artifactId> 
     <version>2.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-jetty-http</artifactId> 
     <version>2.13</version> 
    </dependency> 

    <dependency> 
     <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-json-jackson</artifactId> 
     <version>2.13</version> 
    </dependency> 


    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-server</artifactId> 
     <version>1.9</version> 
    </dependency> 

    <dependency> 
     <groupId>com.sun.jersey.contribs</groupId> 
     <artifactId>jersey-multipart</artifactId> 
     <version>1.9</version> 
    </dependency> 

    </dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>${java.source.level}</source> 
       <target>${java.target.level}</target> 
       <encoding>UTF-8</encoding> 
       <showDeprecation>true</showDeprecation> 
       <showWarnings>true</showWarnings> 
      </configuration> 
     </plugin> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <archive> 
        <manifest> 
         <mainClass>server.SimpleServer</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <appendAssemblyId>false</appendAssemblyId> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

我建我的项目和使用maven命令安装的依赖项:

mvn clean package install 

命令的执行我运行下面的命令服务器后:

java -jar JAXRS-FileUpload-1.0-SNAPSHOT.jar 

我正在控制台上的输出如下:

2014-11-09 00:16:18.011:INFO::main: Logging initialized @83ms 
2014-11-09 00:16:18.067:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT 
2014-11-09 00:16:18.641:INFO:oejsh.ContextHandler:main: Started [email protected]{/,null,AVAILABLE} 
2014-11-09 00:16:18.653:INFO:oejs.ServerConnector:main: Started [email protected]{HTTP/1.1}{0.0.0.0:4343} 
2014-11-09 00:16:18.653:INFO:oejs.Server:main: Started @727ms 

但是,当我试图从网络访问我的应用程序浏览器我收到以下错误消息:

HTTP ERROR: 404 

Problem accessing /. Reason: 

Not Found 
+0

请添加web.xml和您试图访问的网址 – lrnzcig 2014-11-10 13:57:47

+0

我想访问localhost:4343和localhost:4343/upload。我缺少web.xml – Sanja 2014-11-11 10:44:48

回答

2

将您的FileUploadService类添加到一个包(例如com.your.package.name),然后试试下面的(你可以省略在你的代码的完整类名,他们只在这里为清楚起见):

org.glassfish.jersey.server.ResourceConfig rc = new ResourceConfig() {{ 
     packages("com.your.package.name"); 
    }}; 
    org.glassfish.jersey.servlet.ServletContainer container = new ServletContainer(rc); 
    org.eclipse.jetty.servlet.ServletHolder holder = new ServletHolder(container); 
    org.eclipse.jetty.servlet.ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
    context.setContextPath("/foo"); 
    context.addServlet(holder, "/bar"); 

所有资源com.your.package.name应可在http://<host>/foo/bar

与码头9.1测试.3.v20140225和球衣2.7

+0

我仍然收到相同的404错误。我使用1.9球衣和2.13码头。 – Sanja 2014-11-11 10:35:45

+0

您正将'ServletContextHandler'传递给您的Jetty服务器?您是否在服务器启动时看到您的包已扫描注释? – ironchefpython 2014-11-11 16:08:02