Spring Boot Application is a stand-alone solution of a normal Spring Web Application.
It comes with pre-configured conventions of the spring settings.Hence Spring Boot Application needs little configuration and configurations when there is a specific change.
Features:
- Create Stand alone Spring Applications.
- Automatically include Spring configurations/load default configurations.
- In Memory Tomcat Web server for standalone server.
- Provide Production Ready such as metrics/health checks without even adding any extra code.
Requirements: JDK1.7+, Maven 3+
Stack Java. Spring Boot
Like Every Basic Web Application, the Project structure needs to be proper when configuring in Maven, for a Spring Boot Application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<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/xsd/maven-4.0.0.xsd”> <modelVersion>4.0.0</modelVersion> <groupId>my.testwebapp</groupId> <artifactId>Testwebapp</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>My Maven Test Webapp</name> <!– FIXME change it to the project’s website –> <url>http://www.java2use.com</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <!– JSTL for JSP –> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
The above pom.xml needs to have the following SpringBoot and JSP dependencies.
SpringBoot:
org.springframework.boot.spring-boot-starter-web
org.springframework.boot.spring-boot-starter-tomcat
JSTL For JServlet/JSP:
javax.servlet.jstl
org.apache.tomcat.embed.tomcat-embed-jasper
The src folder should have the following three components in project structure for Maven to recognize this as a Web project.
- main/java to store the java sources
- main/resources to store the java resources.
- main/webapp to store the jsp/html or web files.
Java Sources:
A Java class needs to be created for the controller to receive the request and send the response back to the client. Create Stand alone Spring Applications.
SpringBootController.java
1 2 3 4 5 6 7 8 |
package my.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @Controller public class SpringBootController { @RequestMapping(“/index”) String home() { return “index”; } } |
For creating a spring boot application in Java, we have to create a Java file where the Spring application needs to be booted.
WebApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package my.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class WebApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(WebApplication.class); } public static void main(String[] args) throws Exception { SpringApplication.run(WebApplication.class, args); } } |
JSP/HTML Web Files:
A new simple JSP file needs to be created inside webapps for displaying the web page. Lets call index.jsp
index.jsp
1 2 3 4 5 |
<html> <body> <h2>...This is a Index Page…</h2> </body> </html> |
Now we have inform the Spring Boot application about the JSP files, since Spring Boot application for web follows default Spring MVC configuration.
This properties/xml file needs to be created in src/resources folder which will be understood by the Maven.
application.properties
1 2 |
spring.mvc.view.prefix: / spring.mvc.view.suffix: .jsp |
Clean & Build
Once configured, please execute the following instructions to clean & build the Spring Boot Application.
In the Project root directory, please execute the following command.
1 |
mvn clean install |
Running the Spring Boot Application:
Once it is success, we have to start the spring-boot application with another maven command.
1 |
mvn spring-boot:run |
Once Spring Boot Application Started, you will get the logs.., as below, with Port number 8080 where Internal Tomcat server started and running.
1 2 3 4 5 6 |
2019-01-03 17:11:48.719 INFO 13556 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2019-01-03 17:11:48.780 INFO 13556 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-01-03 17:11:48.785 INFO 13556 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2019-01-03 17:11:49.066 INFO 13556 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2019-01-03 17:11:49.160 INFO 13556 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2019-01-03 17:11:49.174 INFO 13556 --- [ main] m.t.r.s.MySpringBootWebApplication : Started MySpringBootWebApplication in 5.509 seconds (JVM running for 13.193) |
Now, once the application started, invoke the URL of JSP, to see the application JSP being invoked from Spring boot server.
1 2 3 |
http://localhost:8080/ or http://localhost:8080/index.jsp |