Reference documents: Springboot RESTful Service Guide


1. The demo

This is a, teach you how to quickly build a Springboot scaffolding web services method, can do a get method interface, comply with RESTful specifications

In the following steps, we will start with how to initialize a scaffold, which will not be covered in the subsequent demo

2. How to build and run the project

1. BySpring initializr websiteDownload a Springboot scaffold

Note: If you forget to add web dependencies, you can manually add the dependency configuration to the pom.xml file and then update Maven. Specific can refer to my another article: juejin.cn/post/703070…

2. Open the project with IDEA and build the project

Note: If the build fails, you need to set Maven to your local download

3. Create the restfulService Package

4. Follow the tutorial to create two files greeting. Java and greetingController.java

4.1 the Greeting. Java

This code creates a standard Greeting class that provides the private variables ID and Content, and sets constructor(), getId(), getContent(), etc., following the style of a standard Java bean class. It makes it easier for other Java classes to get the values of ID and content without directly exposing variables to the public type

package com.example.springbootDemos.restfulService;

public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId(a) {
        return id;
    }

    public String getContent(a) {
        returncontent; }}Copy the code

4.2 GreetingController. Java

This is a controller in MVC that provides RESTful services. On the one hand, the interface can access the new greeting class and on the other hand, it builds the new greeting interface that uses the GET method for easy access.

There are a few points to note:

  • @RestController: indicates that this section uses REST services
  • @GetMapping: indicates that the get method is used. The internal parameter indicates that the interface path is/greeting.
  • Otherwise, if the POST method is used@PostMapping
  • @RequestParamThe key of the requested param is name, and the default string is used if the value is not passedWorld
package com.example.springbootDemos.restfulService;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return newGreeting(counter.incrementAndGet(), String.format(template, name)); }}Copy the code

Note: If two Java files are not in the same folder, import is required when calling

5. Run the restful Service

In the same restfulService folder, create the GreetingApplication.java class, which starts the service using the SpringApplication method

package com.example.springbootDemos.restfulService;

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

@SpringBootApplication
public class GreetingApplication {
    public static void main(String[] args) { SpringApplication.run(GreetingApplication.class, args); }}Copy the code

After clicking Run:

Supplement:

If we want to com. Example. The application in springbootDemos class, to start all kinds of service management

So, we need to edit the contents of the file so that it can start the RESTController above

package com.example.springbootDemos;

// We need to introduce the restful startup method GreetingApplication
import com.example.springbootDemos.restfulService.GreetingApplication;

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

@SpringBootApplication
public class SpringbootDemosApplication {

   public static void main(String[] args) { SpringApplication.run(GreetingApplication.class, args); }}Copy the code

6 Test Results