In this article I’ll describe how to create a simple “Hello World” application with a Spring Model View Controller (MVC) endpoint and a Spring REST endpoint using Spring Boot and the Liberty App Accelerator.

Overview of the app

We’ll create a web application (.war) that runs on the WebSphere Liberty. The app has a static overview home page and two endpoints:

  • A Spring MVC endpoint, http://localhost:9080/myLibertyApp/springbootmvc, which produces a web page displaying Hello, World!.
    The endpoint also accepts an entity request parameter to customize the Hello message; for example, http://localhost:9080/myLibertyApp/springbootmvc/?entity=Bob produces a web page displaying Hello, Bob!.

  • A Spring REST endpoint, http://localhost:9080/myLibertyApp/springbootweb, which returns the plain text: Hello from Spring Boot REST running on Liberty!.

Before you start

Make sure you have Maven and Java installed and configured on your PATH.

Generate a Spring REST app

First we’ll use the Liberty App Accelerator to generate a Spring REST app. On the App Accelerator page:

  1. Select the Spring Boot technology type.
  2. Enter LibertySpringBootProject as the project name.
  3. Click Generate and wait for the project to be created.
  4. Click Download. A .zip file with the same name as the project name is downloaded.
  5. Create a directory called LibertySpringBootProject and extract the LibertySpringBootProject.zip file into it.

A look at the generated code

The generated app has a REST controller, src/main/java/application/springboot/web/LibertyHelloController.java:

package application.springboot.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LibertyHelloController {
    @RequestMapping("/springbootweb")
    public String hello() {
        return "Hello from Spring Boot REST running on Liberty!";
    }
}

The @RestController annotation tells Spring that the class is a REST controller. It has a single REST endpoint, /springbootweb, defined by the @RequestMapping annotation on the hello() method. The hello() method is called when the app is invoked with the URI /springbootweb, which simply returns a plain text string.

Build and run the generated app:

Build and run the app using Maven:

  1. Change to the directory that you extracted the .zip file into:
    cd LibertySpringBootProject
  2. Run the following command to build and run the app:
    mvn install liberty:run-server

    The first time you build the project you may see a large number of messages for Maven dependencies being downloaded. This may take some time so be patient :-). Subsequent builds should be much faster because Maven caches the dependencies.

Maven downloads, installs, and starts a properly configured Liberty server in your project’s target/wlp/ directory.

At the end of of the build (after lots of build messages) you should see a message saying your application (SpringBootLibertyApplication in this example) has started:

[INFO] 2017-05-02 10:25:15.965  INFO 22125 --- [cutor-thread-13] a.s.web.SpringBootLibertyApplication     
            : Started SpringBootLibertyApplication in 2.595 seconds (JVM running for 5.677)

Invoke the generated app

The generated app has a static welcome page you can view at http://localhost:9080/myLibertyApp/:


The app also has a REST endpoint at http://localhost:9080/myLibertyApp/springbootweb. To test it, run the following curl command:

curl http://localhost:9080/myLibertyApp/springbootweb

You should see the plain text message returned:

Hello from Spring Boot REST running on Liberty!

Finally, stop the server by entering Ctrl-C.

Add a Spring Model View Controller endpoint

Now we’ll add an MVC endpoint to the app. To do that we’ll need a new controller class. In the same folder that contains LibertHelloController.java create a LibertyMVController.java class (src/main/java/application/springboot/web/LibertyMVController.java) with the following code:

package application.springboot.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LibertyMVController {
    @RequestMapping("/springbootmvc")
    public String hellomvc(@RequestParam(value="entity", required=false, defaultValue="World") String entity, Model model) {
        model.addAttribute("entity", entity);
        return "hello";
    }
}

A look at the MVC Java code

The @Controller annotation tells Spring that the class is a view controller. The @RequestMapping annotation on the hellomvc() method tells Spring to:

  • Call hellomvc() when the URI /springbootmvc is invoked
  • Pass an HTTP request parameter named entity with a default value of World if the request has no entity parameter
  • Pass a Model object parameter

hellomvc() sets the value of the entity request parameter into the model and returns the name of a view: hello. The class can be any name since the @Controller annotation is all that is needed to identify the controller to Spring.

Add the view

We now have a Controller which sets data in a Model. To complete the MVC pattern we need a View to display the model data. Recall that the controller hellomvc() method returns view name hello. To create the view, create src/main/resources/templates/hello.html with the following HTML:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Liberty App Accelerator MVC Sample</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
        <h1 th:text="'Hello, ' + ${entity} + '!'"/>
        <p>Welcome to your Liberty Spring MVC Application</p>
        <p>Thanks for generating this project using the app accelerator.</p>
    </body>
</html>

The view uses Thymeleaf HTML, which is a template engine for Java, similar to Java Server Pages (JSP). Line 8 in the code above uses the Thymeleaf tag th:text to inject the entity request parameter that was added to the model object by the controller’s hellomvc() method.

To include the necessary Thymeleaf dependencies in the project add the following lines to the <dependencies> section of the pom.xml file in the project directory:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>1.3.0.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Notice that Line 8 in the XML excludes Tomcat because we’ll run the app on Liberty.

Build and invoke the MVC endpoint

To build the changes and start the server, issue the same Maven command as before from the LibertySpringBootProject:

mvn install liberty:run-server

The new MVC endpoint is at http://localhost:9080/myLibertyApp/springbootmvc:


You can change the Hello message with the entity parameter http://localhost:9080/myLibertyApp/springbootmvc?entity=Bob:


Summary

With the Liberty App Accelerator it is fast and easy to create a Spring Boot MVC or REST application (or applications that use other technologies like MicroProfile, Web Sockets, Watson, Swagger, and more).

Join The Discussion

Your email address will not be published. Required fields are marked *