This course will be retired on January 8, 2022.
Persisting a GIF From the Controller7:42 with Chris Ramacciotti
As the final step for uploading a new GIF, in this video we add the necessary controller code to call upon the GifService for saving the uploaded GIF.
GIFs for testing
If you attempt to upload a file larger than 1MB, you'll run into an error. This is because by default, the
MultipartFile's max size is set to 1MB. Check out this Spring guide to tune the max size. Or, download these to stay under 1MB:
Git Command to Sync Your Code to the Start of this Video
git checkout -f s5v4
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse/giflib-hibernate.git
To update your local repository to match a video's starting point, you can use the
git checkout command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
Using a Custom Validator on the Uploaded GIF
Adding validation to the GIF can be a bit tricky, with one reason being that the
MultipartFile is not part of the entity, but should definitely be validated. Currently our
Gif entity uses a
byte field for the GIF's binary image data. We're capturing a
MultipartFile object in the controller method with a
@RequestParam parameter. A nice alternative would be to include the
MultipartFile object as a field in the entity, but we don't want this object to be persisted to the database, so we can mark it as
@Transient (which means it'll exist in our application, but won't be saved to or read from the database).
The advantage of including it as a field is that we can then validate the entire
Gif entity (with the file), and not have to worry about that logic in the controller methods for uploading a new GIF and updating an existing GIF. In order to do this, though, we'll need to write a custom validator.
Check out my Gist on Github to see everything you'll need to make this happen.
You need to sign up for Treehouse in order to download course files.Sign up