Book Image

Mastering Spring MVC 4

By : Geoffroy Warin
Book Image

Mastering Spring MVC 4

By: Geoffroy Warin

Overview of this book

<p>Spring MVC is the ideal tool to build modern web applications on the server side. With the arrival of Spring Boot, developers can really focus on the code and deliver great value, leveraging the rich Spring ecosystem with minimal configuration.</p> <p>Spring makes it simple to create RESTful applications, interact with social services, communicate with modern databases, secure your system, and make your code modular and easy to test. It is also easy to deploy the result on different cloud providers.</p> <p>Mastering Spring MVC will take you on a journey from developing your own web application to uploading it on the cloud.</p> <p>You begin by generating your own Spring project using Spring Tool suite and Spring Boot.</p> <p>As you develop an advanced-level interactive application that can handle file uploads as well as complex URLs, you will dive into the inner workings of Spring MVC and the principles of modern web architectures.</p> <p>You will then test, secure, and optimize your Spring web application and design RESTful services that will be consumed on the frontend.</p> <p>Finally, when everything is ready, you will release your application on a cloud provider and invite everyone to see.</p>
Table of Contents (17 chapters)
Mastering Spring MVC 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

As a web developer, I like to create new things, put them online quickly, and move on to my next idea.

In a world where all our applications are connected to each other, we need to interact with social media to promote our products and complex systems, to provide great value for our users.

Until recently, all this was a distant and complicated world for Java developers. With the birth of Spring Boot and the democratization of cloud platforms, we can now create amazing applications and make them available to everyone in record time, without spending a penny.

In this book, we will build a useful web application from scratch. An application with a lot of neat features, such as internationalization, form validation, distributed sessions and caches, social login, multithreaded programming, and many more.

Also, we will test it completely.

By the end of this book, we will have published our little application and made it available on the Web.

If this sounds like fun to you, let's not waste any more time and get our hands on the code!

What this book covers

Chapter 1, Setting Up a Spring Web Application in No Time, gets us started with Spring Boot really quickly. It covers the tools that will make us more productive, such as Spring Tool Suite and Git. It will also help us to scaffold our application in a snap and see the magic behind Spring boot.

Chapter 2, Mastering the MVC Architecture, guides us through creating a small Twitter search engine. It covers the basics of Spring MVC and the principles of web architecture along the way.

Chapter 3, Handling Forms and Complex URL Mapping, helps you understand how you can create a user profile form. It covers how to validate our data on the server, as well as on the client, and make our application available in different languages.

Chapter 4, File Upload and Error Handling, guides you through adding file upload to your profile form. It demonstrates handling errors properly in a Spring MVC and displaying custom error pages.

Chapter 5, Crafting a RESTful Application, explains the principles of a RESTful architecture. It also helps us to create a user management API accessible through HTTP calls, see which tools can help us design this API, and talks about how we can document it easily.

Chapter 6, Securing Your Application, guides us through securing our application. It covers how we can secure our RESTful API with basic HTTP authentication and our web pages behind a login page. It demonstrates how to enable login via Twitter and store our session on a Redis server to allow our application to scale.

Chapter 7, Leaving Nothing to Luck – Unit Tests and Acceptance Tests, helps us test our application. It discusses testing and TDD, and covers how to unit test our controllers and use modern libraries to design end-to-end tests. It finishes with how Groovy can improve our productivity and the readability of our tests.

Chapter 8, Optimizing Your Requests, takes us through optimizing our application. It covers how to use cache-control and Gzipping. This chapter teaches you how to cache our Twitter search results in-memory and on Redis, and shows you how to multithread the search. As a bonus, implementing Etags and using WebSockets is also covered.

Chapter 9, Deploying Your Web Application to the Cloud, guides us through publishing our application. It shows how the different PaaS solutions can be compared to each other. Then, it demonstrates how to deploy the application on both Cloud Foundry and Heroku.

Chapter 10, Beyond Spring Web, discusses the Spring ecosystem in general, what modern web applications are made of, and where to go from there.

What you need for this book

Although we will build a cutting-edge web application, we do not require you to install a lot of things.

The application that we will build requires Java 8.

You are not forced to, but you definitely should use Git to version control your project. It will be needed if you want to deploy your application on Heroku. Moreover, you will be able to back up your work easily and see the code evolve with the diffs and history. A couple of resources to get started with Git are provided in the first chapter.

I also recommend that you use a good IDE. We will see how to get started quickly with Spring Tool Suite (for free) and IntelliJ Idea (you can obtain a one month trial).

If you have a Mac, you should check Homebrew (http://brew.sh). With this package manager, you can install any tool used in this book.

Who this book is for

This book is perfect for developers who are familiar with the fundamentals of Spring programming and are eager to expand their web development skills. Prior knowledge of the Spring framework is recommended.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "You will find the JAR in the directory build/libs."

A block of code is set as follows:

public class ProfileForm {
    private String twitterHandle;
    private String email;
    private LocalDate birthDate;
    private List<String> tastes = new ArrayList<>();

    // getters and setters
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class ProfileForm {
    private String twitterHandle;
    private String email;
    private LocalDate birthDate;
    private List<String> tastes = new ArrayList<>();

    // getters and setters
}

Any command-line input or output is written as follows:

$ curl https://start.spring.io

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Go to the new project menu and select the Spring Initializr project type ".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can also download the example code for this book at https://github.com/Mastering-Spring-MVC-4/mastering-spring-mvc4.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.