Building APIs

When building HTTP facing APIs the first choice for developing those in the Spring Universe, is by using Spring MVC.

What is Spring MVC?

Spring MVC is Spring Frameworks' answer for developing web applications. It is based around a central DispatcherServlet and controllers implementing the actual request handling. When a request enters the system, it is first handled by the DispatcherServlet, The DispatcherServlet will now determine which controller method is responsible for handling this request and when it find the right one, it forwards the request accordingly. Now, the controller takes over, does whatever logic it contains, returns a result which is then send back as a response. The response can either be plain JSON, XML or we use any of the server side template engines and return an HTML page.

What a controller actually does, is up to us as it is the central point we must provide for building a web application with Spring MVC.

Basics

It's part of Spring Boot: How To Get Started and Build a Microservice and I'll fill in the void here soon.

Securing the API with Spring Security

I covered four common ways for handling security in Spring Boot and Single-Page Applications. The assumes a single-page application aka Javascript in a browsers is the client. However, it coveres Spring Security and is aplicable to others types of clients.

Simple Basic Auth

Getting basic auth on a application is straigth forward. Just add the Spring Security Starter to the pom or gradle.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

This will auto-configure Spring Security to protect your API with basic auth. It will use user as the default username and a randomly generated password on each start of the application. It prints the password in the logs.

However, we can change the username and password with proerties like:

security.user.name=admin
security.user.password=mymightyandhighlysecurepassword

Reliability - Spring Cloud

By using Spring Cloud and NEtflix OSS, we can make our applications or microservices much more reliable.

I cover the usual steps and topics with an example in Spring Boot Intermediate Microservices.

Common Tasks

Returning XML

Sometimes you still need to return XML in your Rest API for various reasons. When you build your API with Spring Boot and Spring MVC, it is a simple, easy task.

Spring MVC: XML Responses in your Rest API

Java 8 Dates in Jackson (JSON)

When you are starting a new Spring Boot application today, you will probably want to use Java 8. However, it still can be tricky with certain frameworks when using new features like the Date and Time API (known as JSR-310).

Handling Java 8 Dates and Time with Jackson (JSR-310)

It is already included in Spring Boot 2. However, sometimes you might need to adjust the date handling anyways.

Uploading Files

Uploading a file with Spring MVC is pretty easy, and it gets even easier when running in Spring Boot. However, it's one of the tasks a developer does once in a while and therefore is pretty easy to forget. The page is a reminder for all of us.

Uploading a File(s) with Spring MVC and Spring Boot

Detecting Mobile Devices

Detecting Mobiles is straight forward with Spring Boot when you know there's a secret module that does the heavy lifting: Spring Mobile.

Detecting a Mobile Device with Spring Boot

Pagination

In this tutorial, I show you an easy way for handling pagination when you use Spring Data and Spring MVC in your application you might not be aware of.

Easy Pagination With Spring Data and MVC

Caching

In this tutorial, we cover the caching feature of Sprring and how to use it in your Spring Boot application. We will use an in-memeory cache and a Redis one.

Caching in Your Spring Boot Application

Quick REST API for Spring Data Backend

There's an easy way to build a REST API for your Spring Data repositories instantly. It doesn't matter if you are using JPA, MongoDB or any of the other stores available with Spring Data. Spring Data REST is the little helper. In this tutorial, we will take a closer look at how you can use it.

Expose Your Spring Data Repositories as a REST API

Tips

In your Spring MVC application, you will usually have at least one endpoint which accepts and ID as a path variable or request parameter, and often the next thing you will do is to load the model from your Spring Data repository. However, there is an easier way as you will see in a minute.

Get Your DB Model as a Method Parameter in Your Controller