简体   繁体   中英

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:

    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 :

<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

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? 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. Consider restricting events using @WithAnnotations or a generic type with bounds. WARN: WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds. WARN: WELD-000411: Observer method [BackedAnnotatedMethod] org.glassfish.sse.impl.ServerSentEventCdiExtension.processAnnotatedType(@Observes ProcessAnnotatedType, BeanManager) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds. WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds. WARN: WELD-000411: Observer method [BackedAnnotatedMethod] private org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds. WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.glassfish.jms.injection.JMSCDIExtension.processAnnotatedType(@Observes ProcessAnnotatedType) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.

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

the solution was the one given by @peeskillet.

My real problem was a little different since I miss to register all the packages with 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);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM