27

I'm quite new to web services so I've started with basic examples. This one relates to file upload. I'm using latest (2.17) version of Jersey bundle for non-maven developers. It states that:

bundle contains the JAX-RS 2.0 API jar, all the core Jersey module jars as well as all the required 3rd-party dependencies

. The problem is that I can not compile this part:

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {
    String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();
    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);
    String output = "File uploaded to : " + uploadedFileLocation;
    return Response.status(200).entity(output).build();
}

It seems that @FormDataParam doesn't exist in Jersey 2.17 bundle although docs says it does. Is the 2.17 bundle incomplete? How can I resolve this problem?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Rasa
  • 337
  • 1
  • 4
  • 10

2 Answers2

44

The bundle only includes the the core modules (and their dependencies). Unfortunately, Multipart is not part of the core. You'll need this dependency (Maven) also

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.17</version>
</dependency

If you're not using Maven, from what I can tell, this artifact only has one other dependency (that is not already included in the bundle), and it's mimepull-1.9.3.

You can download both artifacts below

Nicola Isotta
  • 202
  • 3
  • 10
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Yes, that solved the problem. But, I'm still wondering, why the bundle for non-maven users isn't complete or why there is no links on Jersey site to the missing libs as they are part of Jersey's API docs? – Rasa Mar 24 '15 at 09:40
  • I don't know, but I'm guessing because they suggest using Maven. All the sections in the documentation have Maven dependencies. I simply created a Maven project, and just added the multipart maven dependencies, and see what it pulled in. It is dependent on a lot of jars that are already in the bundle. I just compared and that's how I found the `mimepull` was the only other one needed. It's a hassle, but that's why we use Maven. As far as the why it's not in the bundle, I'm guess because there are alot of other extension projects. If They incuded multipart, then they would have to include all – Paul Samsotha Mar 24 '15 at 09:45
  • of them. And maybe they just wanted to include the core of what is needed, because some things like JSON support have different implementations. It might not make sense to include all those extra third party dependencies, those module are dependent on – Paul Samsotha Mar 24 '15 at 09:46
  • Actually, in the case of multipart at least, if you click on the link in my post, you will actually see that the documentation has a link to Maven repository where are artifact lives, as well as the mimpull – Paul Samsotha Mar 24 '15 at 09:51
  • Now going through some parts of the documentation, I can see that they do actually have a "if you're not using Maven" part below where they show the Maven dependencies, like [here](https://jersey.java.net/documentation/latest/media.html#json.jackson) – Paul Samsotha Mar 24 '15 at 09:53
  • OK. That makes sense. Thanks. Is RESTeasy less dependent of third party libs and maven repos? – Rasa Mar 24 '15 at 10:28
0

You have to download the below dependency from https://mvnrepository.com/ maven repositoryMaven Repository

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart -->
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.31</version>
</dependency>

It is not a part of spring-boot-starter-jersey

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
Prasenjit Mahato
  • 1,174
  • 15
  • 10