【发布时间】:2016-06-30 14:30:17
【问题描述】:
我有一个 jax-rs REST 服务,使用 JEE 7(部署在 glassfish 4 中),它具有处理资源上的 HTTP POST 的方法:
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadVideo(
@FormDataParam("files") InputStream uploadedInputStream,
@FormDataParam("files") FormDataContentDisposition fileDetail) {
try {
//do something
} catch (Exception e) {
e.printStackTrace();
}
return toReturn.build();
}
我在 ejb 中的 pom.xml 是:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.22.2</version>
</dependency>
我使用这些注释注册了其余资源:
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<>();
resources.add(MultiPartFeature.class);
return resources;
}
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put("jersey.config.server.provider.packages", "com.myBean.upload");
return properties;
}
}
当我尝试启动服务器时出现此错误:
java.lang.ClassCastException:无法转换 org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider 到 org.glassfish.jersey.server.spi.ComponentProvider
我已经阅读了这个问题:Jersey 2 injection source for multipart formdata 和 How can I define a JAX-RS service that processes multi-part data in JEE?,但我找不到解决方案。有什么建议吗?
-产生的日志
警告:WELD-000411:观察者方法 [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) 接收所有事件 带注释的类型。考虑使用 @WithAnnotations 或 有边界的泛型类型。警告:WELD-000411:观察者方法 [BackedAnnotatedMethod] 私有 org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) 接收所有带注释类型的事件。 考虑使用 @WithAnnotations 或泛型类型限制事件 有界限。警告:WELD-000411:观察者方法 [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) 接收所有事件 带注释的类型。考虑使用 @WithAnnotations 或 有边界的泛型类型。警告:WELD-000411:观察者方法 [BackedAnnotatedMethod] 公共 org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType) 接收所有带注释类型的事件。 考虑使用 @WithAnnotations 或泛型类型限制事件 有界限。警告:WELD-000411:观察者方法 [BackedAnnotatedMethod] 私有 org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) 接收所有带注释类型的事件。 考虑使用 @WithAnnotations 或泛型类型限制事件 有界限。警告:WELD-000411:观察者方法 [BackedAnnotatedMethod] 公共 org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType) 接收所有带注释类型的事件。 考虑使用 @WithAnnotations 或泛型类型限制事件 有界限。
【问题讨论】:
-
如果将所有依赖项的
<scope>设置为provided会发生什么? -
我尝试了你的建议,现在服务器启动了,但是所有的 Rest 资源都不起作用。他们都以404响应。我不知道为什么(网址和以前一样)
-
您是否看到任何来自泽西岛的服务器日志?我说将范围设置为提供的原因是 Glassfish 已经拥有所有 Jersey jar,并且(版本)可能与您添加到项目中的那些冲突。我看不出 not 提供 jars 会如何导致应用程序无法运行,因为 Glassfish 已经拥有它们。我不知道
-
能否在应用程序类中添加一些日志以查看是否正在创建
-
@peeskillet 我编辑问题以便您查看日志
标签: java web-services rest glassfish jersey-2.0