7

What Jersey (Has RESTful web service support) Version i need to download for JDK 1.6? Please provide the URL of the download and would be helpful. I also need file upload support. What are the jar dependencies for both client and server side?

The REST server code for file upload looks like as follows:

@Path("/file")
public class FileService {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream,@FormDataParam("file") FormDataContentDisposition aFileDetail) {
    OutputStream os = null;
    try {
        File fileToUpload = new File("D:/uploadedFile");
        os = new FileOutputStream(fileToUpload);
        byte[] b = new byte[2048];
        int length;
        while ((length = uploadedInputStream.read(b)) != -1) {
            os.write(b, 0, length);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


}
}

Client code looks like as follows:

Client client = ClientBuilder.newBuilder()
    .register(MultiPartFeature.class).build();
WebTarget webTarget = client.target(REST_SERVICE_URL);
MultiPart multiPart = new MultiPart();

FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file",
    new File("d:/classes12-1.jar"),    
MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);

Response response = webTarget.request(
    MediaType.MULTIPART_FORM_DATA).post(
    Entity.entity(multiPart, multiPart.getMediaType()));
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

2 Answers2

9

You need Jersey version 2.6. Anything after 2.6 is compiled with Java 7. You can download the "bundle" from Maven central. The bundle comes with all the core jars bundled into one jar

The core bundle doesn't come with Multipart support. You need to add the jersey-media-multipart jar. You can down load it from here. Along with this jar, you need one more. If you scroll down in the link, you will see a link for mimepull 1.9.3. You need to download that also.

If you're using Maven, you really only need these two dependencies

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.6</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.6</version>
</dependency>

If the client project is a different project, then in the client project you should just add the below dependency and the above multipart dependency

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.6</version>
</dependency>

If the client project is not using Maven or some other dependency managemnet system, you are going to have a hard time getting all the jars in order, as there is no single jar that contains all the built in jars just for the client. You could just use the above jaxrs-ri jar, but it is a large jar, as it has all the server classes also. So if you need it for say an android client, it might not be a good idea to use. Better just use a dependency manager like Maven or Gradle in this case.

See Also:


UPDATE

If it doesn't work with just the jaxrs-ri-2.6.jar jar, try and download the jaxrs-ri-2.6.tar.gz from the same link above. Unzip it and add all the jars from every directory

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I've added the below jars and not using maven: –  Apr 15 '16 at 14:22
  • You need `jaxrs-ri`, `jersey-media-multipart`, and `mimepull` – Paul Samsotha Apr 15 '16 at 14:29
  • It worked, but server side I'm getting "WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.kumar.FileService.uploadFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0." –  Apr 15 '16 at 15:20
  • Im using tomcat 7.x server –  Apr 15 '16 at 15:21
  • Read the link in the my answer where it says "See Also". You need to register the MultiPartFeature on the server also. Also don't post an answer unless it is an answer. You can edit your original question if you have new information – Paul Samsotha Apr 15 '16 at 15:35
  • It worked in Java 7, but for Java 6 created problems. I used Jersey 1.19 version it worked in Java 6. Upload file also worked. –  Apr 19 '16 at 14:52
  • I added the two dependencies but still getting `java.lang.UnsupportedClassVersionError: UnsupportedClassVersionError: Class org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature has unsupported major or minor version numbers, which are greater than those found in the Java Runtime Environment version 1.6.0_18` – clemtoy Mar 24 '17 at 15:19
  • Official documentation link (section "2.1. Java SE Compatibility"): https://jersey.github.io/documentation/latest/modules-and-dependencies.html#d0e560 - Until version 2.6, Jersey was compiled with Java SE 6. This has changed in Jersey 2.7. (...) – boly38 Jun 22 '17 at 14:51
4

2.1. Java SE Compatibility

2.x branch:

Until version 2.6, Jersey was compiled with Java SE 6. This has changed in Jersey 2.7.

Up to version 2.25.x almost all Jersey components are compiled with Java SE 7 target. It means, that you will need at least Java SE 7 to be able to compile and run your application that is using latest Jersey. Only core-common and core-client modules are still compiled with Java class version runnable with Java SE 6.

Since Jersey 2.26, all modules are build using Java SE 8 and there is no support for running it on older Java SE distributions.