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

后端 未结 8 1501
难免孤独
难免孤独 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:13

    Register MultiPartFeature. In web.xml add to the Jersey servlet:

    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
    </init-param>
    
    0 讨论(0)
  • 2020-11-22 09:22

    I had the same problem when I tried to upload the file. I spent a lot of time until I found a solution to the problem.

    1.If you changed version of your JARs files you may have a version conflicts!

    Clean up your artifacts/libs and rebuild project.

    2.You need to register your UploadFileService class too:

    register(MultiPartFeature.class);
    register(UploadFileService.class);
    

    Hope it will help someone and save your time.

    0 讨论(0)
  • 2020-11-22 09:25

    If someone is using @FormDataParam with @ApiOperation swagger annotation, it won't work(as per swagger latest version at this time) as mentioned here:

    https://github.com/swagger-api/swagger-ui/issues/169

    0 讨论(0)
  • 2020-11-22 09:26

    I too got the same exception.I did the following changes in web.xml

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

    and changed jersey 2.7 to 2.9 .I do not know what change of this 2 has solved the issue.

    0 讨论(0)
  • 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).

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.17</version> <!-- Make sure the Jersey version matches
                                     the one you are currently using -->
    </dependency>
    

    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 <init-param> to the Jersey servlet

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

    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<ExampleConfiguration> 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
    0 讨论(0)
  • 2020-11-22 09:34

    Yet another possible cause for this very generic error is that Jersey only searches for factories associated with the last annotation when multiple ones are declared on a param. (See bug report)

    Until this is fixed, if you are using any other annotations besides @FormDataParam, it has to come last.

    This works:

    @NotEmpty @FormDataParam("myParam") String myParam
    

    This does not:

    @FormDataParam("myParam") @NotEmpty String myParam
    
    0 讨论(0)
提交回复
热议问题