Spring boot establishes restful interface

restful introduction

Introduction to restful architecture:
RESTful architecture is an architecture formed after the improvement of MVC architecture, which is connected with different services by using pre-defined interfaces. In the RESTful architecture, the browser uses POST, DELETE, PUT and GET to add, DELETE, modify and query the specified URL resources respectively. Therefore, RESTful realizes the management and access of resources through URI, which has the characteristics of strong scalability and clear structure. RESTful architecture divides the server into front-end server and back-end server. The front-end server provides users with a modelless view; The back-end server provides an interface for the front-end server. The browser requests the view from the front-end server and initiates an interface request to obtain the model through the AJAX function contained in the view.

Project creation and operation

1. Create a new project, select spring initializr, select default, and click next

2. Pay attention to the Java version. It is recommended to select Java 8 to change the project name

3. Select spring web, and then select the storage path

4. Add package and class after completion, and finally

5. Enter the code in the added class file as follows:

package com.example.springboot.bean;

public class Count {
    private int count;

    public int getCount() {
        return count;

    public void setCount(int count) {
        this.count = count;


package com.example.springboot.controller;

import com.example.springboot.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.springboot.service.springbootService;

public class springbootController {
    springbootService springbootservice;
    @RequestMapping(value = "/me/count", method = RequestMethod.PUT)
    public void initCount(@RequestBody Count count){

    @RequestMapping(value = "/me/count", method = RequestMethod.POST)
    public void modifyCount( @RequestBody Count count){

    @RequestMapping(value = "/me/count", method = RequestMethod.GET)
    public  Count getCount()
        return springbootservice.getCount();


package com.example.springboot.manager;

public class springbootManager {
    private int count = 0;

    private static springbootManager instance = new springbootManager();

    private springbootManager(){}

    public static springbootManager getInstance(){
        return instance;

    public synchronized void addCount(int i){
        count = count + i;

    public synchronized  void minusCount(int i){
        count = count -i;

    public int getCount(){
        return count;

    public void initCount(int i){
        count = i;


package com.example.springboot.service;
import com.example.springboot.bean.Count;
import com.example.springboot.manager.springbootManager;
import org.springframework.stereotype.Service;

public class springbootService {
    public void addCount(Count count){
        if (count != null){

    public void minusCount(Count count){
        if (count != null) {

    public Count getCount()
        Count count = new Count();
        return count;

    public void initCount(Count count){
        if (count != null) {

6. Click Run and the following figure appears, which means success


Through this course, I learned that the function of springboot is very powerful and convenient. At the same time, I learned that some common annotations of springboot:
(1) @ RestController and @ Controller specify a class as the annotation of the Controller and explain their differences
(2) The @ RequestMapping method level mapping annotation is familiar to a small partner who has used Spring MVC
(3) @ EnableAutoConfiguration and @ SpringBootApplication are class level annotations. The correct spring configuration is automatically guessed according to the jar maven depends on. As long as the spring boot starter web dependency is introduced, the Spring MVC and tomcat containers will be automatically configured by default
(4) @ Configuration class level annotation. Generally, this annotation is used to identify the class where the main method is located and complete the initialization of metadata bean s.
(5) @ ComponentScan class level annotation, which automatically scans and loads all Spring components, including Bean injection, and is generally used on the class where the main method is located
(6) @ ImportResource class level annotation. When we have to use an xml Configuration, use @ ImportResource and @ Configuration to identify the class of the file resource.
(7) The @ Autowired annotation is generally combined with the @ ComponentScan annotation to automatically inject a Service or Dao level Bean
(8) The @ Component class level annotation is used to identify a Component. For example, if I have customized a filter, this annotation is required before Spring Boot can correctly identify it.

Tags: Java RESTful http

Posted on Mon, 20 Sep 2021 17:01:36 -0400 by zhaohongli