【问题标题】:Jersey with embedded Jetty server带有嵌入式 Jetty 服务器的泽西岛
【发布时间】:2014-11-08 23:44:21
【问题描述】:

我正在尝试使用嵌入式 Jetty 服务器运行 Jersey 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();


    }
}

我还添加了嵌入式服务器的主要方法:

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   o.e.j.s.ServletContextHandler@2e8e96cd{/,null,AVAILABLE}
2014-11-09 00:16:18.653:INFO:oejs.ServerConnector:main: Started ServerConnector@54e0a229{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

【问题讨论】:

  • 请添加 web.xml 和您尝试访问的 url
  • 我正在尝试访问 localhost:4343 和 localhost:4343/upload。我缺少 web.xml

标签: java maven jersey jetty embedded-jetty


【解决方案1】:

将您的 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://&lt;host&gt;/foo/bar 上可用

使用 jetty 9.1.3.v20140225 和 jersey 2.7 测试

【讨论】:

  • 我仍然遇到同样的 404 错误。我正在使用 1.9 球衣和 2.13 码头。
  • 您将ServletContextHandler 传递给您的Jetty 服务器?您是否看到在服务器启动时扫描您的包以查找注释?
猜你喜欢
  • 2011-12-30
  • 1970-01-01
  • 2015-08-21
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 2016-01-27
  • 2011-12-30
相关资源
最近更新 更多