Categories
Java

Java Spring Boot app with JSP, API and static file endpoints in 5 minutes

We explain how to build a Java Spring Boot application with JSP, API and static file endpoints. Then we run the application locally and test the endpoints with cURL, to check that they respond correctly. All that in less than 5 minutes.

1) The JSP controller

Let’s say we have a JSP file hello.jsp in folder src/main/webapp/WEB-INF/jsp of our Java project, with content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>

and that we want to serve it on endpoint /jsp, with variable ${name} replaced by value John.

To do this, first create a Java class called JspController, with following content:

package com;

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

@Controller
public class JspController {

    @GetMapping("/jsp")
    public String jsp(Model model) {
        model.addAttribute("name", "John");
        return "hello";
    }
}

Then we need to tell Spring Boot how to map “hello” with the JSP file “hello.jsp”. To do this, simply add a file called application.properties in the folder src/main/resources of your Java project. The file application.properties should contain:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

2) The API controller

We also want a Json REST API endpoint /api. To do this, create a Java class called ApiController, with content:

package com;

import com.google.gson.*;
import lombok.*;
import org.springframework.web.bind.annotation.*;

@RestController
public class ApiController {

    public static final Gson gson = new GsonBuilder().create();

    @RequestMapping(value = "/api", method = RequestMethod.GET)
    public String api() {
        MyData data = new MyData("a", 1);
        String json = gson.toJson(data);
        return json;
    }

    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public class MyData {
        public String a;
        public int b;
    }
}

(@NoArgsConstructor@AllArgsConstructor and @Data are annotations from the Lombok project. They add constructors, getters and setters to the Java POJOs, so that we don’t have to write all this boilerplate code. But if you prefer, you can write the getters and setters by hand.).

The main difference between this REST controller and the previous JSP one, is that in the REST controller we use the annotation @RestController, while in the JSP one we used the annotation @Controller.

3) The static file endpoint

To serve static files like CSS style sheets in Spring Boot, simply add them to the folder src/main/resources/static of your Java project. For instance, for a file called style.css with content:

body {
  background-color: lightblue;
}

, and with pathsrc/main/resources/static/style.css in your local Java project, the file will be served on endpoint /style.css by the Spring Boot application.

4) The Spring Boot application

Create one last Java class called Application with following content:

package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

This class is the entry point of our Spring Boot application. Use the function main(String[] args) to run it.

5) Testing the endpoints with cURL

Run Application.main(String[] args) from part 4). The Spring Boot application will run locally on your computer on port 8080. Now using curl in a terminal, you should get the following results:

  • JSP endpoint
$ curl localhost:8080/jsp
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
    <h2 class="hello-title">Hello John!</h2>
</body>
</html>

We see that in the generated HTML above, the variable ${name} from the JSP file was replaced by the value John, which is the behavior we wanted.

  • Json REST API endpoint
$ curl localhost:8080/api
{"a":"a","b":1}
  • Static file endpoint
$ curl localhost:8080/style.css
body {
  background-color: lightblue;
}

Thank you for reading ! If you have a question, please leave a reply below, we reply within 24h.

Leave a Reply

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