Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


1. Introduction

In this tutorial, we consider a new feature of Spring MVC that allows us to specify the web requests using usual Java interfaces.

2. Overview

Usually, when defining a controller in Spring MVC, we decorate its methods with various annotations that specify the request: the URL of the endpoint, the HTTP request method, the path variables, and so on.

We can, for example, introduce the /save/{id} endpoint using said annotations on an otherwise plain method:

public Book save(@RequestBody Book book, @PathVariable int id) {
    // implementation

Naturally, this is not a problem at all when we have just one controller that handles the requests. The situation changes a bit when we have various controllers with the same method signatures.

For example, we might have two different versions of the controller – due to migration or similar – that have the same method signatures. In that case, we'd have a considerable amount of duplicated annotations that accompany the method definitions. Obviously, it would violate the DRY (don't repeat yourself) principle.

If this situation would take place for pure Java classes, we'd simply define an interface and make the classes implement this interface. In the controllers, the main burden on the methods is not due to the method signatures, but due to the method annotations.

Spring 5.1, though, introduced a new feature:

Controller parameter annotations get detected on interfaces as well: Allowing for complete mapping contracts in controller interfaces.

Let's investigate how we can use this feature.

3. Controller's Interface

3.1. Context Setup

We illustrate the new feature by using an example of a very simple REST application that manages books. It'll consist of just one controller with methods that allow us to retrieve and modify the books.

In the tutorial, we concentrate only on the issues related to the feature. All implementation issues of the application can be found in our GitHub repository.

3.2. Interface

Let's define a usual Java interface in which we define not only the signatures of the methods but also the type of web requests they are supposed to handle:

public interface BookOperations {

    List<Book> getAll();

    Optional<Book> getById(@PathVariable int id);

    public void save(@RequestBody Book book, @PathVariable int id);

Notice that we may have a class-level annotation as well as method-level ones. Now, we can create a controller that implements this interface:

public class BookController implements BookOperations {

    public List<Book> getAll() {...}

    public Optional<Book> getById(int id) {...}

    public void save(Book book, int id) {...}


We should still add the class-level annotation @RestController or @Controller to our controller. Defined in this way, the controller inherits all annotations related to the mapping the web requests.

In order to check that the controller now works as expected, let's run the application and hit the getAll() method by making the corresponding request:

curl http://localhost:8081/book/

Even though the controller implements the interface, we may further fine-tune it by adding web request annotations. We may do that in a way as we did it for the interface: either at the class level or at the method level. In fact, we've used this possibility when defining the controller:

public class BookController implements BookOperations {...}

If we add web request annotations to the controller, they'll take precedence over the interface's ones. In other words, Spring interprets the controller interfaces in a way similar to how Java deals with inheritance.

We define all common web request properties in the interface, but in the controller, we may always fine-tune them.

3.3. Caution Note

When we have an interface and various controllers that implement it, we may end up with a situation when a web request may be handled by more than one method. Naturally, Spring will throw an exception:

Caused by: java.lang.IllegalStateException: Ambiguous mapping.

If we decorate the controller with @RequestMapping, we may reduce the risk of ambiguous mappings.

4. Conclusion

In this tutorial, we've considered a new feature introduced in Spring 5.1. Now, when Spring MVC controllers implement an interface, they do this not only in the standard Java way but also inherit all web request related functionality defined in the interface.

As always, we may find the corresponding code snippets on our GitHub repository.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:


Generic footer banner
Comments are closed on this article!