Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Spring Boot 2.0 Cookbook
Spring Boot 2.0 Cookbook

Spring Boot 2.0 Cookbook: Configure, test, extend, deploy, and monitor your Spring Boot application both outside and inside the cloud , Second Edition

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Spring Boot 2.0 Cookbook

Configuring Web Applications

In the previous chapter, we learned about how to create a starting application template, add some basic functionalities, and set up a connection to a database. In this chapter, we will continue to evolve our BookPub application and give it a web presence.

In this chapter, we will learn about the following topics:

  • Creating a basic RESTful application
  • Creating Spring Data REST service
  • Configuring custom servlet filters
  • Configuring custom interceptors
  • Configuring custom HttpMessageConverters
  • Configuring custom PropertyEditors
  • Configuring custom type formatters

Creating a basic RESTful application

While the command-line applications do have their place and use, most of today's application development is centered around web, REST, and data services. Let's start with enhancing our BookPub application by providing it with a web-based API in order to get access to the book catalogs.

We will start where we left off in the previous chapter, so there should already be an application skeleton with the entity objects and a repository service defined and a connection to the database configured.

How to do it...

  1. The very first thing that we will need to do is add a new dependency to build.gradle with the spring-boot-starter-web starter to get us all the necessary libraries for web...

Creating Spring Data REST service

In the previous example, we fronted our BookRepository interface with a REST controller in order to expose the data behind it via a web RESTful API. While this is definitely a quick and easy way to make the data accessible, it does require us to manually create a controller and define the mappings for all the desired operations. To minimize the boilerplate code, Spring provides us with a more convenient way: spring-boot-starter-data-rest. This allows us to simply add an annotation to the repository interface and Spring will do the rest to expose it to the web.

We will continue from where we finished in the previous recipe, and so the entity models and the BookRepository interface should already exist.

How to do it...

...

Configuring custom servlet filters

In a real-world web application, we almost always find a need to add facades or wrappers to service requests, to log them, filter out bad characters for XSS, perform authentication, and so on. Out of the box, Spring Boot automatically adds OrderedCharacterEncodingFilter and HiddenHttpMethodFilter, but we can always add more. Let's see how Spring Boot helps us achieve this task.

Among the various assortments of Spring Boot, Spring Web, Spring MVC, and others, there is already a vast variety of different servlet filters that are available and all we have to do is define them as beans in the configuration. Let's say that our application will be running behind a load balancer proxy and we would like to translate the real request IP that is used by the users instead of the IP from the proxy when our application instance receives the request...

Configuring custom interceptors

While servlet filters are a part of the Servlet API and have nothing to do with Spring besides being automatically added in the filter chain --Spring MVC provides us with another way of wrapping web requests: HandlerInterceptor. According to the documentation, HandlerInterceptor is just like a filter. Instead of wrapping a request in a nested chain, an interceptor gives us cutaway points at different phases, such as before the request gets handled, after the request has been processed, before the view has been rendered, and at the very end, after the request has been fully completed. It does not let us change anything about the request, but it does let us stop the execution by throwing an exception or returning false if the interceptor logic determines so.

Similar to using filters, Spring MVC comes with a number of premade HandlerInterceptors. The...

Configuring custom HttpMessageConverters

While we were building our RESTful web data service, we defined the controllers, repositories, and put some annotations on them; but nowhere did we do any kind of object translation from the Java entity beans to the HTTP data stream output. However, behind the scenes, Spring Boot automatically configured HttpMessageConverters so as to translate our entity beans into a JSON representation written to HTTP response using the Jackson library. When multiple converters are available, the most applicable one gets selected based on the message object class and the requested content type.

The purpose of HttpMessageConverters is to translate various object types into their corresponding HTTP output formats. A converter can either support a range of multiple data types or multiple output formats, or a combination of both. For example, MappingJackson2HttpMessageConverter...

Configuring custom PropertyEditors

In the previous example, we learned how to configure converters for an HTTP request and response data. There are other kinds of conversions that take place, especially in regards to dynamically converting parameters to various objects, such as Strings to Date or an Integer.

When we declare a mapping method in a controller, Spring allows us to freely define the method signature with the exact object types that we require. The way in which this is achieved is via the use of the PropertyEditor implementations. PropertyEditor is a default concept defined as part of the JDK and designed to allow the transformation of a textual value to a given type. It was initially intended to be used to build Java Swing / Abstract Window Toolkit (AWT) GUI and later proved to be a good fit for Spring's need to convert web parameters to method argument types...

Configuring custom type formatters

Mostly because of its statefulness and lack of thread safety, since version 3, Spring has added a Formatter interface as a replacement for PropertyEditor. The formatters are intended to provide a similar functionality but in a completely thread-safe manner and focusing on a very specific task of parsing a String in an object type and converting an object to its String representation.

Let's suppose that for our application, we would like to have a formatter that would take the ISBN number of a book in a String form and convert it to a book entity object. This way, we can define the controller request methods with a Book argument when the request URL signature only contains an ISBN number or a database ID.

How to do it...

...
Left arrow icon Right arrow icon

Key benefits

  • • This collection of effective recipes serves as guidelines for Spring Boot application development
  • • Get up to date with features of the latest version of Spring Boot 2.0
  • • Tips and tricks to improve your efficiency through the stages of software development

Description

The Spring framework provides great flexibility for Java development, which also results in tedious configuration work. Spring Boot addresses the configuration difficulties of Spring and makes it easy to create standalone, production-grade Spring-based applications. This practical guide makes the existing development process more efficient. Spring Boot Cookbook 2.0 Second Edition smartly combines all the skills and expertise to efficiently develop, test, deploy, and monitor applications using Spring Boot on premise and in the cloud. We start with an overview of the important Spring Boot features you will learn to create a web application for a RESTful service. Learn to fine-tune the behavior of a web application by learning about custom routes and asset paths and how to modify routing patterns. Address the requirements of a complex enterprise application and cover the creation of custom Spring Boot starters. This book also includes examples of the new and improved facilities available to create various kinds of tests introduced in Spring Boot 1.4 and 2.0, and gain insights into Spring Boot DevTools. Explore the basics of Spring Boot Cloud modules and various Cloud starters to make applications in “Cloud Native” and take advantage of Service Discovery and Circuit Breakers.

Who is this book for?

This book is for Java Developers who have good knowledge and understanding of Spring and Java application development.

What you will learn

  • • Get to know Spring Boot Starters and create custom auto-configurations
  • • Work with custom annotations that enable bean activation
  • • Use DevTools to easily develop and debug applications
  • • Learn the effective testing techniques by integrating Cucumber and Spock
  • • Observe an eternal application configuration using Consul
  • • Move your existing Spring Boot applications to the cloud
  • • Use Hashicorp Consul and Netflix Eureka for dynamic Service Discovery
  • • Understand the various mechanisms that Spring Boot provides to examine an application's health

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 26, 2018
Length: 286 pages
Edition : 2nd
Language : English
ISBN-13 : 9781787123670
Vendor :
Pivotal
Category :
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Feb 26, 2018
Length: 286 pages
Edition : 2nd
Language : English
ISBN-13 : 9781787123670
Vendor :
Pivotal
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 110.97
Learning Spring Boot 2.0
€36.99
Spring 5.0 By Example
€36.99
Spring Boot 2.0 Cookbook
€36.99
Total 110.97 Stars icon

Table of Contents

10 Chapters
Getting Started with Spring Boot Chevron down icon Chevron up icon
Configuring Web Applications Chevron down icon Chevron up icon
Web Framework Behavior Tuning Chevron down icon Chevron up icon
Writing Custom Spring Boot Starters Chevron down icon Chevron up icon
Application Testing Chevron down icon Chevron up icon
Application Packaging and Deployment Chevron down icon Chevron up icon
Health Monitoring and Data Visualization Chevron down icon Chevron up icon
Spring Boot DevTools Chevron down icon Chevron up icon
Spring Cloud Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
(3 Ratings)
5 star 0%
4 star 33.3%
3 star 0%
2 star 0%
1 star 66.7%
Sanjay Kumar J Aug 23, 2019
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
Not for professionals
Amazon Verified review Amazon
Keith Lancaster Jun 16, 2018
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
I can't tell you anything about the book, since I download the Kindle preview to check it out and it STOPS at the table of contents! NO sample text.
Amazon Verified review Amazon
Amazon Customer Mar 20, 2018
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Very helpful book. Concepts explained very well. One should really refer the book to have concepts clear. One to recommend.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.