Step 1:
Create basic Skelton
project.
Go to 'https://start.spring.io/'
and give the Group as 'com.sample', Artifact as 'app' and add below
dependencies.
a.
Spring
Web Starter
b.
H2
Database
c.
Spring
Data JPA
d.
Spring
Boot Actuator
e.
Spring
Boot DevTools
Click on
the button ‘Generate the project’.
Extract
the downloaded zip file.
Step 2:
Export the project to
Eclipse.
Open
Eclipse.
File ->
Import.
Select ‘Existing Maven Projects’. Click on Next button.
Browse to
the location where you extracted the zip file, click on Finish button.
Step 2:
Create a package
‘com.sample.app.controller’ and define HelloController.
HelloController.java
package com.sample.app.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/")
public String home() {
return "Welcome to spring boot developement";
}
}
AppApplication.java
package com.sample.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Run
AppApplication.java.
Open the
url ‘http://localhost:8080/actuator’ in browser, you can see below kind of
response.
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-component": {
"href": "http://localhost:8080/actuator/health/{component}",
"templated": true
},
"health-component-instance": {
"href": "http://localhost:8080/actuator/health/{component}/{instance}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
}
}
}
Adding
security to the application
Add spring
security Starter dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Add below
properties to application.properties file.
application.properties
spring.security.user.name=krishna spring.security.user.password=password123 spring.security.user.roles = ["USER", "ADMIN"]
Stop and
restart the application.
Open the url 'http://localhost:8080/actuator' in
browser, you will be navigated to login page.
Enter username as krishna and password as password123 and click on Sign in button. You will be navigated to actuator screen.
You can
download the complete working application from this link.
No comments:
Post a Comment