Monday, May 28, 2018

Spring Boot Maven Tomcat

You got Started with Apache Maven and you got Started with Spring Boot. Now you’re ready for the next step, so let’s build a complete front to back website with Maven and Spring.



Create Maven Web App

From the command-line you can generate a new project with Maven Archetypes. Archetypes are skeleton applications that you can build from. In our case I want to create a Web Application, so I can use the ‘maven-archetype-webapp’ archetype.
$ mvn archetype:generate -DgroupId=com.remkohde.hello -DartifactId=hello -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
$ cd hello
Open the ‘hello’ web app in your favorite IDE. Edit the ‘pom.xml’,
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.remkohde.hello</groupId>
<artifactId>hello</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Hello Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
  <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
  <build>
<finalName>hello</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration></configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>mytomcat7</server>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
This adds the ‘tomcat7-maven-plugin’ to run in an embedded Tomcat7 server. Run the webapp with ‘mvn tomcat7:run’ and open the webapp in ‘http://localhost:8080/’.
$ mvn tomcat7:run

Add a Servlet

Edit the pom.xml to add a dependency to support for HttpServlet in the ‘javax.servlet’ package of J2EE.
<dependencies>
<!--
https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api
-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Create a new file ‘~/src/main/java/com/remkohde/hello/HelloServlet.java’, which creates a ‘HelloServlet.java’ class in the ‘com.remkohde.hello’ package in the default Java sources folder ‘~/src/main/java’ in your web server. Edit the ‘HelloServlet.java’ class.
package com.remkohde.hello;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Hello Servlet</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Hello Servlet");
out.println("</BODY>");
out.println("</HTML>");
}
}
Lastly, configure Tomcat to map to our HelloServlet. Edit the ‘~/src/main/webapp/WEB-INF/web.xml’.
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Hello World</display-name>
  <servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
  <servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
Restart Tomcat with ‘mvn tomcat7:run’ and open the Servlet in ‘http://localhost:8080/hello’.

Add Spring

We did a lot of basic J2EE work in a very short time, but let’s step it up. The real way to build a J2EE Web Application is with Spring. So let’s convert our web application into a Spring Boot web application. In Spring, HTTP requests are handled by a controller. So-called annotation replaces the ‘web.xml’ servlet definition and mapping, and Spring boots the servlets by inspecting your classes and looking for this annotation.
Edit your ‘pom.xml’ to support Spring.
<dependencies>
...
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
...
</dependencies>
...
<build>
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.0.RELEASE</version>
</plugin>
</plugins>
</build>
Add 2 Java classes: ‘~/src/main/java/com/remkohde/hello/Application.java’ and ‘~/src/main/java/com/remkohde/hello/HelloController.java’.
Add the entry point or the default main class for our Spring Application.
package com.remkohde.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The HelloController is in Spring land what our HelloServlet was in a pure J2EE Servlet world.
package com.remkohde.hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
}
The ‘return “hello”;’ line returns a view called ‘hello’ which refers to a file ‘hello.html’ in the ‘~/src/main/resources/templates/’ folder. The view template style I am using here is Thymeleaf, which is a dependency in the ‘pom.xml’. Other view templates you can use are JSP/JSTL, Tiles or Velocity.
Create the view template ‘~/src/main/resources/templates/hello.html’.
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello ' + ${name} + '!'" />
</body>
</html>
Wow! Done! You did it. Let’s run your Tomcat Web App with Maven and Spring!
$ mvn spring-boot:run
Open the application in ‘http://127.0.0.1:8080/hello’. You should see ‘Hello World!’. Let’s try to say hi to yourself: ‘http://127.0.0.1:8080/hello?name=Remko’.

No comments:

Post a Comment