REST - Jetty 9和Jersey 2.6 Multipart文件上传

时间:2018-02-14 10:59:15

标签: java jersey jetty

我尝试使用Jetty 9.4.7Jersey 2.26创建一个api来编写(上传)文件。

api的结构如下:/api/write

我正在使用App.java包中的com.rest.app初始化服务器。资源文件FileUploadResource.java位于com.rest.res包中。

这是App.java班级:

public class App {
  public static void main(String[] args) {
    Server server = new Server(8500);

    final ResourceConfig resourceConfig = new ResourceConfig(FileUploadResource.class);
    resourceConfig.packages("com.rest.res");
    resourceConfig.register(MultiPartFeature.class);

    ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    contextHandler.setContextPath("/");
    server.setHandler(contextHandler);

    ServletHolder jerseyServletHolder = contextHandler.addServlet(ServletContainer.class, "/api/*");
    jerseyServletHolder.setInitOrder(0);


    try {
        server.start();
        server.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Stopping server....");
        server.destroy();
    }

}

}

这里是FileUploadResource.java

@Path("/")
public class FileUploadResource {
    @POST
    @Path("write")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response write(@FormDataParam("file") InputStream fileInputStream,@FormDataParam("file") FormDataContentDisposition contentDisposition) {

        // upload file
        // do something

        return response;
    }
}

当我发送任何请求时,我收到此错误:

WARNING: Attempt to release request processing resources has failed for a request.
java.lang.NoSuchMethodError: javax.servlet.http.HttpServletResponse.getStatus()I

此处的pom.xml依赖关系树:

<dependencies>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-server</artifactId>
        <version>9.4.7.v20170914</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-servlet</artifactId>
        <version>9.4.7.v20170914</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.26</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.26</version>
    </dependency>
</dependencies>

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的pom:

@Query(“SELECT * FROM Users”)
fun getUsers(): List<User>