Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Spring MVC Cookbook
Spring MVC Cookbook

Spring MVC Cookbook: Over 40 recipes for creating cloud-ready Java web applications with Spring MVC

Arrow left icon
Profile Icon Alexandre Bretet Profile Icon Alex Bretet
Arrow right icon
€32.99 €36.99
Full star icon Half star icon Empty star icon Empty star icon Empty star icon 1.5 (2 Ratings)
eBook Feb 2025 466 pages 1st Edition
eBook
€32.99 €36.99
Paperback
€45.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Alexandre Bretet Profile Icon Alex Bretet
Arrow right icon
€32.99 €36.99
Full star icon Half star icon Empty star icon Empty star icon Empty star icon 1.5 (2 Ratings)
eBook Feb 2025 466 pages 1st Edition
eBook
€32.99 €36.99
Paperback
€45.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€32.99 €36.99
Paperback
€45.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
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Spring MVC Cookbook

Chapter 2. Designing a Microservice Architecture with Spring MVC

In this chapter, we will cover the following topics:

  • Configuring a controller with simple URL mapping
  • Configuring a fallback controller using the ViewResolver
  • Setting up and customizing a responsive single page webdesign with Bootstrap
  • Displaying a model in the View using the JSTL
  • Defining a common WebContentInterceptor
  • Designing a client-side MVC pattern with AngularJS

Introduction

You need to complete the first chapter before starting this new one. The first chapter installs the basics for the trading platform we are building. It also creates a modular toolkit that every recipe will be using.

This second chapter sets the product on an acceleration ramp. It will shape the whole chain of responsibilities and draft the big picture of the Microservice architecture. Once more, we will establish a necessary structure for the chapters to come, but on another level.

The User eXperience paradigm

For a couple of years now, we've assisted an amazingly active frontend revolution. Since the rise of HTML 5 and CSS3, with the common development platforms for mobile (iOS, Android, and so on), and with the amount of connected devices, so many doors and opportunities have been opened to the developer communities. The frequency of new JavaScript libraries popping-up in the open source field has made it quite difficult to follow.

But it's a revolution for good! It...

Configuring a controller with simple URL mapping

This recipe introduces the Spring MVC controller with its simplest implementation.

Getting ready

We will discover later on, and especially in Chapter 3, Working with Java Persistence and Entities, that Spring MVC is a great tool to build a REST API. Here, we will focus on how to create a controller that prints some content in the response.

Starting with this recipe, we will be using GIT to follow each iteration that has been made to develop the cloudstreetmarket application. After the initial setup, you will appreciate how smoothly you can upgrade.

How to do it...

This recipe comes with two initial sections for installing and configuring GIT.

Downloading and installing GIT

  1. To download GIT, go to the GIT download page at https://git-scm.com/download. Select the right product corresponding to your environment (Mac OS X, Windows, Linux, or Solaris).
  2. To install GIT for Linux and Solaris, execute the suggested installation commands using the system&apos...

Configuring a fallback controller using ViewResolver

This recipe introduces some more advanced concepts and tools related to Controllers such as ViewResolvers, URI Template Patterns, and Spring MVC's injection-as-argument. The recipe is quite simple but there is more to talk about.

Getting ready

We will keep working from the same codebase state as the previous recipe where we have pulled the v2.2.1 tag from the remote repository. It will only be about creating one Controller with its handler method.

How to do it...

  1. In the cloudstreetmarket-webapp module and in the package edu.zipcloud.cloudstreetmarket.portal.controllers, the following DefaultController has been created:
    @Controller
    public class DefaultController {
      @RequestMapping(value="/*", method={RequestMethod.GET,RequestMethod.HEAD})
      public String fallback() {
        return "index";
      }
    }

    Note

    We will explain in detail how this method-handler serves as a fallback interceptor.

  2. Access the http://localhost:8080/portal...

Setting up and customizing a responsive single page webdesign with Bootstrap

Bootstrap is a UI Framework initially created by Mark Otto and Jacob Thornton at Twitter. It is an amazing source of styles, icons, and behaviors, abstracted to define and enrich components. Bootstrap offers an easy, rational, and unified set of patterns for defining styles. It had no equivalent before. If you have never used it, you will be excited to get so much visual feedback from a quick definition of the DOM.

In June 2014 it was the number 1 project on GitHub with over 73,000 stars and more than 27,000 forks. Their documentation is very fluid and easy to go through.

Getting ready

In this recipe, we will use Bootstrap to set up the web-design basics for our CloudStreet Market project from an existing Bootstrap theme. We will remake the index.jsp page to render a better looking welcome page that can be previewed with the following screenshot.

Getting ready

How to do it...

There are three major steps in this recipe:

  • Installing...

Displaying a model in the View, using the JSTL

This recipe shows how to populate the Spring MVC View with data and how to render this data within the View.

Getting ready

At this point, we don't have any real data to be displayed in our View. For this purpose, we have created three DTOs and two service layers that are injected from their Interface into the controller.

There are two dummy service implementations that are designed to produce a fake set of data. We will use the Java Server Tags Library (JSTL) and the JSP Expression Language (JSP EL) to render the server data in the right places in our JSP.

How to do it...

  1. After checking out the v2.x.x branch (in the previous recipe), a couple of new components are now showing-up in the cloudstreetmarket-core module: two interfaces, two implementations, one enum, and three DTOs. The code is as follows:
    public interface IMarketService {
      DailyMarketActivityDTO getLastDayMarketActivity(String string);
      List<MarketOverviewDTO> getLastDayMarketsOverview...

Defining a common WebContentInterceptor

In this recipe, we will highlight how we have implemented a WebContentInterceptor superclass for Controllers.

Getting ready

We are about to present a Controller superclass having the specificity of being registered as a WebContentInterceptor. This superclass allows us to globally control sessions and to manage caching options.

It will help us understanding the request lifecycle throughout the Framework and through other potential interceptors.

How to do it...

  1. Registering a default WebContentInterceptor with its specific configuration can be done entirely with the configuration approach:
    <mvc:interceptors>
      <bean id="webContentInterceptor" class="org.sfw.web.servlet.mvc.WebContentInterc	eptor">
        <property name="cacheSeconds" value="0"/>  
        <property name="requireSession" value="false"/>  
        ...
      </bean>
    <mvc:interceptors>

    Tip

    In our application, we have...

Designing a client-side MVC pattern with AngularJS

This recipe explains the installation and the configuration of AngularJS to manage a single-page web application.

Getting ready

In this recipe, we explain how we got rid of the rendering logic introduced previously in the JSPs to build the DOM. We will now rely on AngularJS for this job.

Even if we don't have yet a REST API that our frontend could query, we will temporarily make the JSP build the needed JavaScript objects as if they were provided by the API.

AngularJS is an open source Web application Framework. It provides support for building single-page applications that can directly accommodate microservice architecture requirements. The first version of AngularJS was released in 2009. It is now maintained by Google and an open source community.

AngularJS is a whole topic in itself. As a Framework, it's deep and wide at the same time. Trying to present it as a whole would take us beyond the scope of this book and wouldn't...

Left arrow icon Right arrow icon

Key benefits

  • Configure Spring MVC to build logic-less controllers that transparently support the most advanced web techniques
  • Build an amazing social and financial application that applies microservices patterns on deployment, self-testability, interoperability, cloud architectures, and scalability
  • Fast-paced, practical guide to learn how to set up Spring MVC to produce REST resources and templates as required by the latest front-end best practices

Description

Spring MVC is a lightweight application framework that comes with a great configuration by default. Being part of the Spring Framework, it naturally extended and supported it with an amazing set of recognizable annotations. External libraries can be plugged in and plugged out. It also possesses a request flow. Complete support of REST web services makes the Spring architecture an extremely consistent choice to support your front-end needs and Internet transformations. From the design of your Maven modules, you will achieve an Enterprise-standard for a stateless REST application based on Spring and Spring MVC with this book. This guide is unique in its style as it features a massive overview of practical development techniques brought together from the Spring ecosystem, the new JEE standards, the JavaScript revolution and Internet of Things. You will begin with the very first steps of Spring MVC's product design. Focused on deployment, viability, and maintainability, you will learn the use of Eclipse, Maven, and Git. You will walk through the separation of concerns driven by the microservices principles. Using Bootstrap and AngularJS, you will develop a responsive front-end, capable of interacting autonomously with a REST API. Later in the book, you will setup the Java Persistence API (JPA) within Spring; learn how to configure your Entities to reflect your domain needs, and discover Spring Data repositories. You will analyze how Spring MVC responds to complex HTTP requests. You will implement Hypermedia and HATEOAS to guide your customer's stateless conversation with the product and see how a messaging-service based on WebSocket can be configured. Finally you will learn how to set up and organize different levels of automated-tests, including logging and monitoring.

Who is this book for?

If you are an experienced Java developer, with prior experience in web technologies, and want to step up in your career and stay up-to-date or learn more about Spring Web scalability, this book is for you.

What you will learn

  • * Structure your project with Maven and create self-tested, domain-specific deployable web archives
  • * Generate templates for a responsive and powerful frontend with AngularJS and Bootstrap
  • * Build a high performance stateless RESTful and hypermedia application to support your multiple customer experiences
  • * Authenticate over REST with a BASIC authentication scheme and OAuth2; handle roles and permissions
  • * Document and publish your REST API using Swagger and Swagger UI
  • * Scale your Spring web application
  • * Communicate through WebSocket and STOMP messages
  • * Provide support to your application and efficiently maintain its business features with a relevant test stack

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Last updated date : Feb 11, 2025
Publication date : Feb 25, 2016
Length: 466 pages
Edition : 1st
Language : English
ISBN-13 : 9781784394103
Languages :
Tools :

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
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Last updated date : Feb 11, 2025
Publication date : Feb 25, 2016
Length: 466 pages
Edition : 1st
Language : English
ISBN-13 : 9781784394103
Languages :
Tools :

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 112.97
Spring MVC Cookbook
€45.99
Spring MVC Blueprints
€41.99
Building a RESTful Web Service with Spring
€24.99
Total 112.97 Stars icon

Table of Contents

10 Chapters
1. Setup Routine for an Enterprise Spring Application Chevron down icon Chevron up icon
2. Designing a Microservice Architecture with Spring MVC Chevron down icon Chevron up icon
3. Working with Java Persistence and Entities Chevron down icon Chevron up icon
4. Building a REST API for a Stateless Architecture Chevron down icon Chevron up icon
5. Authenticating with Spring MVC Chevron down icon Chevron up icon
6. Implementing HATEOAS Chevron down icon Chevron up icon
7. Developing CRUD Operations and Validations Chevron down icon Chevron up icon
8. Communicating Through WebSockets and STOMP Chevron down icon Chevron up icon
9. Testing and Troubleshooting Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Half star icon Empty star icon Empty star icon Empty star icon 1.5
(2 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 50%
1 star 50%
Heath Mar 17, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I have not gotten too far in this book because I have to spend way too much time troubleshooting the examples. So far every one has been broken at some point.
Amazon Verified review Amazon
Jorge F. Reyes-Spindola Aug 28, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
This book is shoddily written as to be useless. The examples are badly and incompletely explained and there is very little explanation about the components and their interrelationship. In general, the quality of Packt Publishing books is abysmal. This is the third or fourth book from Packt that I've tried to use to learn a new technology and they're useless. It's obvious that the publishers are doing this to make a quick buck out of the people who want or need to learn the latest and greatest programming technologies. My recommendation is to stay away from books published by Packt or from Manning.
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.

Modal Close icon
Modal Close icon