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

谁说我不能喝 提交于 2019-11-28 17:13:48
Michal Gajdos

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).

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.

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");
Rauan Argyn

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;
    }
}

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

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) {
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!