This is the second tutorial about Spring Boot and MVC. In the first tutorial we have learnt how to create a simple MVC applicaiton with Spring Boot and ThymeLeaf for the view: Creating an MVC application using Spring Boot. In this tutorial we will enhance our example with REST Controller so that our application can be accessed also with REST API.

RESTful web services in Spring Boot are a common pattern for developing web applications as they are significantly easier as you don't have to deal with the HTML and CSS. The beauty is that you can apply the same MVC pattern which we have learned for web applications.

One advantage of Spring Boot is that is natively supports JSON, but you can also bring XML onto the classpath so that you can support both or only XML if you so prefer.

So let's take a look at building a RESTful web service in our application. Now let's recap our domain class:

package com.example;

public class Person {

	private String name;
	private String surname;

	public Person(String name, String surname) {
		super();
		this.name = name;
		this.surname = surname;
	}

	public Person() {
		super();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

}

We're going to add a @Service Bean that can be inject into both my application and the web service.

package com.example;

import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class PersonService {
    private static List<Person> persons = new ArrayList<>();

    static{
		Person p1 = new Person("Jack", "Smith");
		Person p2 = new Person("Lucas", "Derrik");
		Person p3 = new Person("Andy", "Miller");
		
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
    }

    public List<Person> getAllPersons(){
        return persons;
    }
}

As you can see, our Service class has a static block which is used to add some Person objects and a List method which returns the List of Person. Let's go ahead an build the RestController:

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ApiController {

    private PersonService service;

    @Autowired
    public ApiController(PersonService service){
        super();
        this.service = service;
    }

    @GetMapping("/persons")
    public List<Person> getAllPersons(){
        return this.service.getAllPersons();
    }
}

As you can see, the Service class is @Autowired in the Constructor of the ApiController Rest class and we expose the List of Person via the GetMapping "/persons". So we will now respond at "/api/persons" with the list of Person object.

Now we'll go ahead and start up our web application and navigate to http://localhost:8080/api/persons. You'll see that I a nice XML payload is returned:

Spring MVC pattern Thymeleaf tutorial

Please notice that in order to enable XML representations, Jackson XML extension (jackson-dataformat-xml) must be present on the classpath. Add the following dependency to your project:

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

Choosing the Resource representation

A resource can have multiple representations:

  • XML
  • HTML
  • JSON

In the above example, the browser sent a request for an XML representation of data. If a consumer, however, sends a request with Accept header as ‘application/json’, we need to provide the JSON representation of the resource. Let's see an example using cURL:

$ curl -H "Accept: application/json" http://localhost:8080/api/persons

[{"name":"Jack","surname":"Smith"},{"name":"Lucas","surname":"Derrik"},{"name":"Andy","surname":"Miller"}]

Here is how to request again the XML representation of data from shell:

curl -H "Accept: application/xml" http://localhost:8080/api/persons
<List><item><name>Jack</name><surname>Smith</surname></item><item><name>Lucas</name><surname>Derrik</surname></item><item><name>Andy</name><surname>Miller</surname></item></List> 
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials