Friday 5 April 2024

Spring boot 3 : Hello World rest application

In this post, I am going to explain how to create a hello world rest application using spring boot3.

Prerequisites

Java17

Maven 3.5 or higher (I tested the app with maven 3.8.4)

 

Step 1: Create new maven project ‘spring3-hello-world’.

 

Step 2: Update pom.xml with maven dependencies.

 

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>spring3-hello-world</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<maven.compiler.source>17</maven.compiler.source>
		<maven.compiler.target>17</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.2</version>
	</parent>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springdoc</groupId>
			<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
			<version>2.0.4</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Define SwaggerConfig class.

 

SwaggerConfig.java

package com.sample.app.config;

import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;

@Configuration
@OpenAPIDefinition(info = @Info(title = "Hello service Application", version = "v1"))
public class SwaggerConfig {

}

 

Step 4: Define HelloController class.

 

HelloController.java

package com.sample.app.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin("*")
@RequestMapping(value = "/apis/v1")
public class HelloController {
	
	@GetMapping("/hello")
	public ResponseEntity<String> sayHello(){
		return ResponseEntity.ok("Hello World");
	}

}

 

Step 5: Define main application class.

 

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

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

}

Total project structure looks like below.


 

Build and generate the artifice

Go to the folder, where pom.xml is located and execute below command.

 

mvn package

 

Upon command success, you can see spring3-hello-world-0.0.1-SNAPSHOT.jar file in the application target folder.

 

Execute below command to start the application.

 

java -jar ./target/spring3-hello-world-0.0.1-SNAPSHOT.jar

 

Once the application started, open the below url in browser.

 

http://localhost:8080/swagger-ui/index.html

 

You will see below kind of screen.

 

Expand the api ‘/apis/v1/hello’ to confirm the api.

 

You can download this application from this link.


 

 


 

Previous                                                 Next                                                 Home

No comments:

Post a Comment