Uploading file using Jersey over RESTfull service and The resource configuration is not modifiable?

前端 未结 6 973
误落风尘
误落风尘 2020-12-13 04:15
@Path(\"file.upload\")
public class UploadFileService {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam(\"file\") I         


        
相关标签:
6条回答
  • 2020-12-13 04:41

    If you are using jetty server and jersey servlet, then you can solve this problem by adding the following code in your main class where you have started the jetty server,

    ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/*"); jerseyServlet.setInitOrder(0);

          // Tells the Jersey Servlet which REST service/classes to load.
          jerseyServlet
                  .setInitParameter(
                          "jersey.config.server.provider.classnames",
                          <Your entry point class's canonical name>
                                  + ";org.glassfish.jersey.media.multipart.MultiPartFeature");
    
    0 讨论(0)
  • 2020-12-13 04:41

    Just minor clarification

    Use

    import org.glassfish.jersey.media.multipart.MultiPartFeature
    import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
    import org.glassfish.jersey.media.multipart.FormDataParam;
    

    Not

    com.sun.jersey.*
    

    Did work for me only when used org.glassfish.jersey.media.multipart.*

    In ApplicationConfig just register MultiPartFeature as

    import org.glassfish.jersey.media.multipart.MultiPartFeature;
    
    @javax.ws.rs.ApplicationPath("webresources")
    public class ApplicationConfig extends Application {
    
        @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> resources = new java.util.HashSet<>();
            resources.add(UploadFileService.class);
            resources.add(MultiPartFeature.class);
            return resources;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 04:45

    I had the same problem and wanted to avoid creating a custom application class. It is not well documented, but if you want to add Multipart functionality, all you have to do is add this to your web.xml jersey servlet config:

    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>
    

    I also added a loggingfilter.

    0 讨论(0)
  • 2020-12-13 04:53

    I'm using Jersey 1.9.1. org.glassfish...... works well with Jersey 2. For Jersey 1 you better use com.sun... classes.

    0 讨论(0)
  • 2020-12-13 04:56

    In order to use multipart in your Jersey application you need to register MultiPartFeature in your application, i.e.:

    public class ApplicationConfig extends Application {
    
        public Set<Class<?>> getClasses() {
            final Set<Class<?>> resources = new HashSet<Class<?>>();
    
            // Add your resources.
            resources.add(UploadFileService.class);
    
            // Add additional features such as support for Multipart.
            resources.add(MultiPartFeature.class);
    
            return resources;
        }
    }
    

    For more information see Multipart section in the Jersey Users Guide.

    For the second issue you're facing try to restart the GlassFish server, I am not sure how NetBeans are reloading the Jersey app after a change (if this doesn't help, please post your ApplicationConfig).

    0 讨论(0)
  • 2020-12-13 05:02

    You can use @FormDataParam("file") equivalent of FormDataMultiPart if you want it using annotation.

    Used as given below sample code extract:

    public Response uploadFile( **@FormDataParam("file")** InputStream fileInputStream,
                 @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
    
    0 讨论(0)
提交回复
热议问题