我使用Jersey 2开发了一个使用RESTful API在Jetty上运行的Web服务。 我后来不得不创建一个文件上传方法(主要用于XLS / XML文件),我尝试使用Jersey 2的Multipart库。
但是,当服务器启动时,它会立即抛出一个奇怪的异常:
2016-09-21 01:13:13.578:INFO:oejs.AbstractConnector:main: Started ServerConnector@17f62e33{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/inject/ExtractorException
at org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider.createValueFactory(FormDataParamValueFactoryProvider.java:436)
[.............................................]
public static void main(String[] args) {
final ResourceConfig resourceConfig = new ResourceConfig(API.class);
resourceConfig.packages("the_package_where_these_classes_are");
resourceConfig.register(MultiPartFeature.class);
ServletHolder jerseyServlet
= new ServletHolder(new ServletContainer(resourceConfig));
Server jettyServer = new Server(8080);
ServletContextHandler context = new ServletContextHandler(jettyServer, "/");
context.addServlet(jerseyServlet, "/*");
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
e.printStackTrace();
} finally {
//jettyServer.destroy();
// got an IllegalStateException uncommenting this and didn't quite understand why
}
}
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
@Path("/")
public class API {
@POST
@Path("test")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetails) throws Exception {
System.out.println(fileDetails.getFileName());
return Response.ok().build();
}
}
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import javax.ws.rs.core.Application;
import java.util.Set;
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(MultiPartFeature.class);
resources.add(API.class);
return resources;
}
}
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.11.v20160721</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
</dependencies>
我使用了一些错误的依赖吗?这个简单的服务不应该起作用吗?我之前从未使用过Multipart,所以我尝试了几个互联网资源/教程。
提前致谢!
固定
我在所有Jersey依赖项中将版本更改为最新版本并且它有效!
答案 0 :(得分:0)
jersey-media-multipart
取决于jersey-common
,添加
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.23.2</version>
</dependency>
答案 1 :(得分:0)
对我来说,工作如下:
final ResourceConfig resourceConfig = new ResourceConfig(ApplicationConfig.class);
resourceConfig.packages("com.econorma.rest");
resourceConfig.register(MultiPartFeature.class);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(resourceConfig));
这是ApplicationConfig类
@ApplicationPath("/")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add(MultiPartFeature.class);
resources.add(EntryPoint.class);
return resources;
}
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("jersey.config.server.provider.packages", "com.econorma.rest");
return properties;
}
}
答案 2 :(得分:0)
Jersey提供了相应版本的示例。您可以在项目中添加以下依赖项,并使用IDE的下载源代码功能可以下载此示例的源代码。通过单击pom父级,您还可以看到相同球衣版本的所有示例。它有帮助!
<dependency>
<groupId>org.glassfish.jersey.examples</groupId>
<artifactId>multipart-webapp</artifactId>
<version>${jersey.version}</version>
</dependency>