DEV Community

Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on • Edited on

3

Hello World in Spring Boot

Alt Spring Boot

Spring Boot [Rest API] - Hello World

Hello World App developed using Spring Boot.

Installation

Import Maven based project in any of your Favourite IDE.

./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Output

Open in Browser

http://localhost:5000/hello-world
Enter fullscreen mode Exit fullscreen mode
http://localhost:5000/helloworld-bean
Enter fullscreen mode Exit fullscreen mode

Usage

HelloWorldController.java

helloWorld() method returning a string "Hello World".
The @GetMapping annotation is used to handle the GET request with the URI provided. It is a different version of @RequestMapping specifically for the GET Request.

@GetMapping("/hello-world")
    public String helloWorld() {
        return "Hello World";
    }
Enter fullscreen mode Exit fullscreen mode

helloWorldBean() method returning a bean with string parameter "Hello World". The bean is nothing but the instance object which are managed by the spring container. These beans are kind of stored at a one place like bag of beans from where they are accessed whenever required. The object created by the container are called beans.

@GetMapping("/helloworld-bean")
    public HelloWorldBean helloWorldBean() {
        return new HelloWorldBean("Hello World");
    }
Enter fullscreen mode Exit fullscreen mode

helloWorld1() method printing Hello World, 'name' using @PathVariable annotation. The @PathVariable annotation is used to extract the parameter value from the URI provided in this case name is being extracted from the URI and then it is called by the HelloWorldBean.

@GetMapping("/helloworld/{name}")
    public HelloWorldBean helloWorl1(@PathVariable String name) {
        return new HelloWorldBean(String.format("Hello World, %s", name));
    }
Enter fullscreen mode Exit fullscreen mode

Credits

in28Minutes

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay