Step 1: Create a simple maven project using the maven “archetype” plugin.
1 2 |
mvn -B archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-spring-boot |
Step 2: Import it as an “existing maven project” into eclipse.
Step 3: The pom.xml file to bring in the Spring boot dependencies & plugins.
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 57 58 59 60 61 62 63 64 |
<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.mytutorial</groupId> <artifactId>simple-spring-boot</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>simple-spring-boot</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <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-actuator</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <!-- Build an executable JAR --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <mainClass>com.mytutorial.App</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
Step 4: The “App.java” with a RESTful endpoint.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.mytutorial; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @Controller public class App { @ResponseBody @RequestMapping("/myapp") public String hello() { return "Hello from RESTful Web Service"; } public static void main( String[] args ) { SpringApplication.run(App.class, args); //runs in an Application server like Tomcat } } |
Step 5: Create a new “source folder” named “src/main/resources”.
Step 6: Create “application.properties” under “src/main/resources”.
1 2 3 4 5 |
endpoints.beans.id=springbeans endpoints.metrics.sensitive=false endpoints.env.sensitive=false endpoints.shutdown.enabled=true |
Step 7: Build the jar (i.e uber jar) with
1 |
mvn clean package |
or
1 |
mvn clean install |
Step 8: Start the application with embedded tomcat.
1 2 |
c:\projects\simple-spring-boot>java -jar target\simple-spring-boot-1.0-SNAPSHOT.jar |
Test with WizTools RESTClient
http://localhost:8080/myapp the endpoint that we built via “App.java”
http://localhost:8080/health production ready endpoint created via spring boot starter actuator.
http://localhost:8080/metrics production ready endpoint created via spring boot starter actuator.
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 |
{"mem":352536, "mem.free":185105, "processors":8, "instance.uptime":413701, "uptime":417734, "systemload.average":-1.0, "heap.committed":303104, "heap.init":262144, "heap.used":117998, "heap":3702784, "nonheap.committed":52864, "nonheap.init":2496, "nonheap.used":49434, "nonheap":0, "threads.peak":24, "threads.daemon":20, "threads.totalStarted":27, "threads":22, "classes":6124, "classes.loaded":6124, "classes.unloaded":0, "gc.ps_scavenge.count":6, "gc.ps_scavenge.time":42, "gc.ps_marksweep.count":1, "gc.ps_marksweep.time":35, "httpsessions.max":-1, "httpsessions.active":0, "gauge.response.myapp":31.0, "gauge.response.health":78.0, "counter.status.200.health":1, "counter.status.200.myapp":1} |
http://localhost:8080/env production ready endpoint created via spring boot starter actuator.