Spring Boot Web application example

This post show you how to build a basic web application with Spring MVC + Spring Boot using Spring Tool Suite (STS).

First will be created a base starting project from Eclipse, then a very simple controller will be added returning a string as view content, and finally will be show how to serve an htmlfile from the controller.

Create the project in Eclipse

  1. Open Eclipse STS.
  2. Right click on the project panel (or go on Menu > File) then New > Spring Starter Project.
  3. Fill the following fields in the form:
    • Name: the eclipse project name (e.g. “spring-boot-basewebapp”).
    • Group: a group name for your projects (e.g. “netgloo”).
    • Artifact: the name of the jar file (e.g. “spring-boot-basewebapp”).
    • Description: a brief human readable description (e.g. “Spring Boot base Web Application”).
    • Package name: the java root package name (e.g. “netgloo”).

    Note: Group and Artifact are from Maven, and here there is a brief explanation if you want to follow their conventions: http://maven.apache.org/guides/mini/guide-naming-conventions.html.

  4. Click Next. Search and select the “Web” dependency, then click Finish.
  5. Will be downloaded a basic project template from the Spring web site.

Now you should get a project structure like this:

basewebapp/
  + src/
  |  + main/
  |  |  + java/
  |  |  |   + netgloo/
  |  |  |   |  - Application.java
  |  |  + resources/
  |  |  |   |  - application.properties
  |  + main/
  |  + test/
  + target/
  - pom.xml

 

Sample code

  1. Create a new folder “controllers” in “src/main/java/netgloo” and add a java class “MainController” inside that folder:
    src/main/java/netgloo/controllers/MainController.java
    

    containing the following code:

    package netgloo.controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class MainController {
    
      @RequestMapping("/")
      @ResponseBody
      public String index() {
        return "Hello World!!!";
      }
    
    }
  2. Right click on the project folder > Run As… > Spring Boot App
  3. Open your Web Browser and go to http://localhost:8080/. You should see the text “Hello World!!!” returned by the index method.

Serving an html page

  1. Create a new folder “static” inside “src/main/resources” and create a file “hello.html”:
    src/main/resources/static/hello.html
    

    containing:

    <!DOCTYPE html>
    <html>
    <body>
      <h2>Hello world!</h2>
    </body>
    </html>
  2. Remove the @ResponseBody annotation and specify the file that will be returned (“hello.html”) on the controller class BasicController:
    package netgloo.controllers;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class BasicController {
    
      @RequestMapping("/")
      public String index() {
        return "hello.html";
      }
    
    }
  3. Start the web server application (Run As… > Spring Boot App).
  4. Open your Web Browser and go to http://localhost:8080/. Now you should see the new content from “hello.html” file.

You can also put static resources inside the directory

src/main/resources/static/

Dynamic content

In order to serve dynamic content you can choose between jsp or thymealef. Spring Boot allows you to use both without any configuration files!

Here there is an example using thymealeaf:
http://spring.io/guides/gs/serving-web-content/

and here an example using jsp:
https://github.com/mariuszs/spring-boot-web-jsp-example

GitHub

You can get the whole code used in this post from our GitHub repository:
https://github.com/netgloo/spring-boot-samples/tree/master/spring-boot-basewebapp

 

RESOURCES:

 

SPRING BOOT WEB APPLICATION

 

SPRING BOOT OFFICIAL