MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response

后端 未结 8 1589
难免孤独
难免孤独 2020-11-22 08:52

I am using Jersey based restful Service implementation strategy to build a service which will be used to upload files. My service class name is : UploadFileService.java (See

8条回答
  •  一生所求
    2020-11-22 09:29

    Get rid of jersey-multipart-1.18.jar. That is for Jersey 1.x. Add these two

    • jersey-media-multipart-2.17
    • mimepull-1.9.3

    For Maven you would use the following dependency (you don't need to explicitly add the mimepull dependency, as this one will pull it in).

    
        org.glassfish.jersey.media
        jersey-media-multipart
        2.17 
    
    

    Then you need to register the MultiPartFeature. If you are using a ResourceConfig for configuration, you can simply do

    register(MultiPartFeature.class);
    

    If you are using web.xml, then you can add the class as an to the Jersey servlet

    
        jersey.config.server.provider.classnames
        org.glassfish.jersey.media.multipart.MultiPartFeature
    
    

    Note that if you have multiple providers that you want to register, then you can delimit each provider class with a comma or semicolon. You cannot use this same param-name twice. See Suarabh's answer

    UPDATE

    Also, once you get rid of jersey-multipart-1.18.jar you will have compile errors for the missing imported classes. For the most part, the class names are still the same, just the packages have changed, i.e.

    • org.glassfish.jersey.media.multipart.FormDataParam
    • org.glassfish.jersey.media.multipart.FormDataContentDisposition

    For Dropwizard

    If you're using Dropwizard, instead of adding the jersey-media-multipart, they document for your to add dropwizard-forms instead. And instead of registering the MultiPartFeature, you should register the MultiPartBundle

    @Override
    public void initialize(Bootstrap bootstrap) {
        bootstrap.addBundle(new MultiPartBundle());
    }
    

    Really doesn't make much difference though as all the Dropwizard bundle does is register the MultiPartFeature with the ResourceConfig.


    Aside

    If you are here for a different ModelValidationException, here are some links for information on other causes of the exception.

    • 1
    • 2
    • 3

提交回复
热议问题