简体   繁体   中英

Jersey client file upload exception: NoClassDefFoundError: org/glassfish/jersey/message/internal/HeadersFactory

im trying to create a jersey client to upload files. I`ve searched the i-net but i cant find a proper solution for my exception.

This is my client code:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

//...

private static void clientUploadTest() {
    ClientConfig config = new ClientConfig();
    Client client       = ClientBuilder.newClient(config);
    client.register(MultiPartFeature.class);
    URI baseUri         = UriBuilder.fromUri("http://localhost/rest/test").build();
    WebTarget target    = client.target(baseUri);

    MultiPart multiPart = new MultiPart();

}

If i try to add the multiPart, i got the following exception. I tried different ways but i fix my problem. i also tried something like this:

final FileDataBodyPart filepart     = new FileDataBodyPart("file", new File("C:/Users/admin/Downloads/testtext.txt"));
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
final FormDataMultiPart multiPart   = (FormDataMultiPart) formDataMultiPart.field("foo", "bar").bodyPart(filepart);

but i got the same error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/message/internal/HeadersFactory at org.glassfish.jersey.media.multipart.BodyPart.<init>(BodyPart.java:70) at org.glassfish.jersey.media.multipart.MultiPart.<init>(MultiPart.java:66) at de.restclient.requests.ClientTest.clUploadtest(ClientTest.java:64) at de.restclient.requests.ClientTest.main(ClientTest.java:46) Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.message.internal.HeadersFactory at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 4 more

And this are my imported jersey files.

Jersey files

Im able to send normal GET and Post Request to the server. But i need to send files too. I hope, someone can help me.

缺少依赖项

org.glassfish.jersey.media:jersey-media-multipart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM