[英]Rest Service Jax-Rs - Post Multipart/data
I have a jax-rs REST service, using JEE 7 (deployed in glassfish 4), which has a method to process HTTP POST on the resource: 我有一个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();
}
My pom.xml in ejb is : 我在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>
And I registered the rest resource with these annotations: 我用这些注释注册了其余的资源:
@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;
}
}
When I try to start the server I get this error: 当我尝试启动服务器时,我收到此错误:
java.lang.ClassCastException: Cannot cast org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider to org.glassfish.jersey.server.spi.ComponentProvider java.lang.ClassCastException:无法将org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider强制转换为org.glassfish.jersey.server.spi.ComponentProvider
I already read this questions : Jersey 2 injection source for multipart formdata and How can I define a JAX-RS service that processes multi-part data in JEE? 我已经读过这个问题: 针对multipart formdata的Jersey 2注入源以及如何定义在JEE中处理多部分数据的JAX-RS服务? but I cannot find a solutions. 但我找不到解决方案。 Any suggestions? 有什么建议么?
-Log produced - 生产日志
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) receives events for all annotated types. 警告:WELD-000411:Observer方法[BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType,BeanManager)接收所有带注释类型的事件。 Consider restricting events using @WithAnnotations or a generic type with bounds. 考虑使用@WithAnnotations限制事件或使用带边界的泛型类型。 WARN: WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. 警告:WELD-000411:Observer方法[BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType)接收所有带注释类型的事件。 Consider restricting events using @WithAnnotations or a generic type with bounds. 考虑使用@WithAnnotations限制事件或使用带边界的泛型类型。 WARN: WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) receives events for all annotated types. 警告:WELD-000411:Observer方法[BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType,BeanManager)接收所有带注释类型的事件。 Consider restricting events using @WithAnnotations or a generic type with bounds. 考虑使用@WithAnnotations限制事件或使用带边界的泛型类型。 WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. 警告:WELD-000411:Observer方法[BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType)接收所有带注释类型的事件。 Consider restricting events using @WithAnnotations or a generic type with bounds. 考虑使用@WithAnnotations限制事件或使用带边界的泛型类型。 WARN: WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. 警告:WELD-000411:Observer方法[BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType)接收所有带注释类型的事件。 Consider restricting events using @WithAnnotations or a generic type with bounds. 考虑使用@WithAnnotations限制事件或使用带边界的泛型类型。 WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. 警告:WELD-000411:Observer方法[BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType)接收所有带注释类型的事件。 Consider restricting events using @WithAnnotations or a generic type with bounds. 考虑使用@WithAnnotations限制事件或使用带边界的泛型类型。
Finally I got the solution. 最后我得到了解决方案。 For the first problem : 对于第一个问题:
Cannot cast org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider to org.glassfish.jersey.server.spi.ComponentProvider 无法将org.glassfish.jersey.ext.cdi1x.transaction.internal.TransactionalExceptionInterceptorProvider转换为org.glassfish.jersey.server.spi.ComponentProvider
the solution was the one given by @peeskillet. 解决方案是@peeskillet给出的解决方案。
My real problem was a little different since I miss to register all the packages with Rest Resource. 我真正的问题有点不同,因为我错过了使用Rest Resource注册所有软件包。 I post here the solution for this problem anyway: 我在这里发布了这个问题的解决方案:
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add(MultiPartFeature.class);
return resources;
}
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<>();
String array[] = {"com. myBean.home","com. myBean.upload","com.bandyer.search","com.bandyer.mail"};
properties.put("jersey.config.server.provider.packages", array);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.