@FormDataParam throws SEVERE: Missing dependency

2020-04-20 11:51发布

问题:

I am using Jersey to upload file. I defined the method:

@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Collection<Message> uploadImage(@FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {
}

And invoke the call from Jersey Client for testing:

ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
Client client = Client.create(cc);
String url = "http://localhost:8080/API/api/images/upload/150";
File f = new File("C:/Pictures/1360_435x300.jpg");
FormDataMultiPart form = new FormDataMultiPart();
form.bodyPart(new FileDataBodyPart("thumbnail", f));
String s = client.resource(url).type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.APPLICATION_JSON).post(String.class, form);

However, I get:

SEVERE: Missing dependency for method public java.util.Collection ImageResource.uploadImage(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) throws java.io.IOException at parameter at index 1

what is wrong?

回答1:

Make sure all libs are the same version (e.g. 1.13), and don't forget to add mimepull.jar and jersey-multipart.jar as well.



回答2:

Thanks @john 4d5 People please ensure that all the jars version are in sync You can find all the possible jars here. :) https://maven.java.net/index.html#welcome



回答3:

You need to add Maven dependencies related to Multipart handling.

    <dependency> <!-- choose your version -->
        <groupId>org.jvnet</groupId>
        <artifactId>mimepull</artifactId>
        <version>1.6</version>
    </dependency>
    <dependency> <!-- choose your version -->
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.18.1</version>
    </dependency>

If you need check http://mvnrepository.com/ for the versions of the dependencies for your Jersey version.



标签: java rest jersey