Home > AI > Backend > SpringBoot >

Intro

Step 1: install the plugin Spring Assistance

Step 2: start a empty Spring project

Step 3: copy the code to the DemoApplication.java file in the src/main/java/com/example/demo folder.

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
              
              @SpringBootApplication
              @RestController
              public class DemoApplication {
                
                  
                  public static void main(String[] args) {
                  SpringApplication.run(DemoApplication.class, args);
                  }
                  
                  @GetMapping("/hello")
                  public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
                  return String.format("Hello %s!", name);
                  }
                
              }
            

The hello() method we’ve added is designed to take a String parameter called name, and then combine this parameter with the word "Hello" in the code. This means that if you set your name to “Amy” in the request, the response would be “Hello Amy”.

The @RestController annotation tells Spring that this code describes an endpoint that should be made available over the web. The @GetMapping(“/hello”) tells Spring to use our hello() method to answer requests that get sent to the http://localhost:8080/hello address. Finally, the @RequestParam is telling Spring to expect a name value in the request, but if it’s not there, it will use the word “World” by default.

Step 4: run the application

 Open a command line (or terminal) and navigate to the folder where you have the project files.

# Mac
./mvnw spring-boot:run

Step 5: check this url

http://localhost:8080/hello
http://localhost:8080/hello?name=amy

You can check this post: https://spring.io/quickstart

Leave a Reply