RESTEasy (WildFly – JAX-RS 2.1) file handling

Last Updated:  February 26, 2020 | Published: April 21, 2019

In my latest blog post, I demonstrated a solution for up- and downloading files with Jersey (JAX-RS 2.1) on Payara. As WildFly does not rely on Jersey as the JAX-RS reference implementation and is using RESTEasy instead, I'll show you a quick example for file handling with RESTEasy on WildFly.

Setting up the backend using RESTEasy

For this blog post, I'm using a classic Java EE 8 Maven application with RESTEasy's multipart-provider as a provided and additional dependency (no need to package the library within the .war as WildFly already contains this lib):

The JAX-RS configuration is pretty straightforward:

Next, for storing the uploaded file, I'm using a simple JPA entity called FileUpload:

The file handling within the JAX-RS endpoint method is not as straightforward as with Jersey as you have to work with the MultipartFormDataInput interface and manually extract the filename and content-type:

RESTEasy file handling: extract the filename

For extracting the filename I wrote a small helper method which parses the HTTP header Content-Disposition:

Similarly, extracting the content type of the file is even simpler:

The required code for downloading a file does not depend on any proprietary RESTEasy and is just JAX-RS and Java EE standard (in the example I'm offering a random file for the user to download):

You'll find the full code base on GitHub with an HTML page to upload files and test the implementation.

Furthermore, if you are looking for more examples to handle files (up- and download):

Phil

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Sign up for Our Mailing List And Get

    the Testing Java Applications ($9) Cheat Sheet for Free

    Testing Java Applications Cheat Sheet Cover
    >