我尝试使用Jetty 9.4.7
和Jersey 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>
我做错了什么?
感谢。
答案 0 :(得分:0)
尝试将此添加到您的pom:
@Query(“SELECT * FROM Users”)
fun getUsers(): List<User>