Build a springboot project in 3 minutes and run it

first step:

Create a maven project.

Step two:

Import maven dependencies, the code is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zy</groupId>
    <artifactId>hello-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!-- springboot工程需要继承的父工程   -->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.7</version>
    </parent>


    <!--web开发的起步依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>



</project>

third step:

Define the controller package.

The project structure is as follows:

package com.zy.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class hellocontroller {
    @RequestMapping("/hello")
    public String hello () {
        return "hello spring boot !";
    }
}

The role of the RestController annotation :

The @RestController annotation is used to identify a class as a controller that handles REST requests. It is a way to quickly develop RESTful Web services provided by Spring MVC.

The specific functions include:
1. Automatically convert the object returned by the method in the Controller into a JSON or XML response, and return it to the client as an HTTP response.
2. According to the requested HTTP method (GET, POST, PUT, DELETE, etc.) and the path of the request, the request is assigned to the corresponding method for processing.
3. Various request mapping annotations (such as GetMapping, PostMapping, PutMapping, DeleteMapping, etc.) can be used to define specific request processing methods.
4. Support processing and returning data in different formats, such as JSON, XML, etc.
5. It is convenient to define and process RESTful interfaces.

In short, the role of the @RestController annotation in Java is to identify a class as a controller that handles REST requests, which facilitates rapid development and processing of RESTful Web services.

 RequestMapping annotation:

The @RequestMapping annotation is one of the annotations used to map HTTP requests in Java. It can be applied at the class level and method level to specify a controller class or method to handle a specific URL request.

At the class level, the @RequestMapping annotation can be used to specify the base path of the URLs handled by the controller class. For example, you can use @RequestMapping("/users") to map a controller class to handle requests for URLs starting with /users.

At the method level, the @RequestMapping annotation can be used to specify the URL path and HTTP request method for processing a specific request. For example, @RequestMapping(value = "/{id}", method = RequestMethod.GET) can be used to map a method to a request that handles GET requests with a URL path of /{id}.

In addition to specifying the URL path and HTTP request method, the @RequestMapping annotation also supports other configuration options, such as request parameters, request headers, response media types, etc. These options can be specified via the @RequestParam and @RequestHeader annotations.

Overall, the @RequestMapping annotation is one of the important annotations in Java for mapping HTTP requests. It serves to specify controller classes and methods to handle requests for specific URLs.

the fourth step:

Write a bootstrap class. Usually the application ends:

package com.zy;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
//引导类
@SpringBootApplication
public class helloApplication {
    public static void main(String[] args) {
        SpringApplication.run(helloApplication.class, args);
    }
}

SpringBootApplication annotation:

@SpringBootApplication is one of the core annotations of Spring Boot, which is used to identify the main class of a Spring Boot application. This annotation includes the functionality of several other annotations to simplify the configuration and deployment of Spring Boot applications.

The @SpringBootApplication annotation has the following functions:

1. @Configuration: Mark the class as a configuration class, allowing Spring to scan and load the Bean definitions in it.
2. @EnableAutoConfiguration: Turn on the automatic configuration function, and automatically configure the components required by the Spring application according to the dependencies of the project.
3. @ComponentScan: Enable component scanning, automatically find the beans that Spring needs to manage and register them in the application context.
4. @SpringBootConfiguration: Spring Boot-specific configuration annotations, indicating that this class is a Spring Boot configuration class.
5. @Import: Introduce other configuration classes or components and add them to the context of Spring applications.

By using the @SpringBootApplication annotation, you can reduce a lot of configuration code and simplify the startup and deployment process of Spring Boot applications. Just add this annotation on the main class, and Spring Boot can identify and load the configuration and components required by the application through automatic configuration and component scanning.

It should be noted that the @SpringBootApplication annotation is usually placed on the main class of the application, which is used to start the Spring Boot application. If the main class is not in the top-level package, you need to additionally specify the basePackages attribute of the @ComponentScan annotation so that Spring Boot can scan and load components correctly.

the fifth step:

Project begining. The effect is as follows:

 Go to the page to see:

Guess you like

Origin blog.csdn.net/m0_62600503/article/details/131839772