Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.0modelVersion>
	groupId>com.sample.appgroupId>
	artifactId>spring3-hello-worldartifactId>
	version>0.0.1-SNAPSHOTversion>

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

	parent>
		groupId>org.springframework.bootgroupId>
		artifactId>spring-boot-starter-parentartifactId>
		version>3.2.2version>
	parent>

	dependencies>

		dependency>
			groupId>org.springframework.bootgroupId>
			artifactId>spring-boot-starter-webartifactId>
		dependency>

		dependency>
			groupId>org.springdocgroupId>
			artifactId>springdoc-openapi-starter-webmvc-uiartifactId>
			version>2.0.4version>
		dependency>
	dependencies>

	build>
		plugins>
			plugin>
				groupId>org.springframework.bootgroupId>
				artifactId>spring-boot-maven-pluginartifactId>
			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 ResponseEntitysayHello(){
		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


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Spring boot 3 : Hello World rest application

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×