“Like it” and make it a habit.

Message: The long wind and waves will sometimes, straight sail to the sea. This article has been included at github.com/likekk/-Blo… Welcome everyone star, learn together and make progress together. If there are any mistakes in the article, please point them out. The route of front-end learning and resource sharing will be planned on GitHub in the later stage.

preface

Before SpringBoot, we built the SSM(SpingMVC+Spring+Mybatis) project. When we built the SSM project, we had to go through a series of tedious configurations, such as: Configuration information for application,web. XML, Spring-servlet, etc. If we get one of these configurations wrong. Then we are faced with looking for a lot of bugs, and there are some abnormalities that we can not understand, for comrades who are not good at English, this is internal injury. So what problem does SpringBoot solve? To put it simply, SpringBoot mainly simplifies our configuration operations, packaging those things we need to configure, we use it, its benefits are as follows:

  • Create a stand-alone Spring application

  • Embedded Tomcat without the need to deploy a WAR file

  • Simplifying Maven Configuration

  • Automatic Spring configuration

  • Provides production-ready functions such as metrics, health checks, and external configuration

  • There is absolutely no code generation and no configuration requirements for XML

Project structures,

1. Create a SpringBoot Project, open the development tool IDEA, and select Create New Project


2. Select Spring Initializr and click Next


3. Here is some knowledge about Maven, because the blogger has not written about Maven blog for the time being, I hope you can understand, the blogger will make up in the future, directly click next


4. This part of the interface has many dependencies, when we need to use it in the later development, we can choose, now we do not choose anything, directly click Next


5. Click Finish to complete a simple SpringBoot project


6. The simple SpringBoot project structure is as follows


7. Each SpringBoot project has a main program, which can be directly started. Here we do not need to configure Tomcat, and the main program structure is as follows


8. We can run the project directly. The default SpringBoot port is port 8080, which can be modified through the configuration file later


At this point, there is nothing, don’t worry, let’s create a new controller, and then add some static data simulation database. Between adding the controller, we need to add some dependencies. This dependency belongs to the Web part, and we have no choice which part to rely on directly at the beginning, so we will actively add dependencies.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

9. The directory structure and controller code are as follows


package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 import java.util.HashMap; import java.util.Map;  @RestController public class IndexController {  @RequestMapping("/index")  public Map index(a){  Map map=new HashMap();  map.put("name"."A stray KK.");  map.put("type"."The male ape");  map.put("sex"."male");  return map;  } } Copy the code

10. Now we are ready, click Run and type localhost:8080/index in the address bar.


Don’t worry, there is a particularly important step left to write, I found that many bloggers have not written, I will fill in their holes here.


11. Remember that every SpringBoot project has a main entry, so let’s go to the main entry and configure it. Add a new annotation, and I’ll explain the purpose of each annotation in more detail later in the blog. I will not introduce them here.


If localhost:8080/index is displayed, you have successfully completed your first SpringBoot project. At this point we see that the data is ready to be displayed.


@RestControllerVS@Controller

  • @RestController is a combination of @Controller and @responseBody, so when you annotate @RestController in a Controller, the whole Controller returns JSON and you can’t return a view, If you want to go back to the view you can use @Controller

  • So when we’re using @Controller, we’re going to return a view, and if we want to return JSON, we’re going to put @ResponseBody above the method that needs to return JSON, and we’re going to return JSON.

At the end

If you find this post useful, please click the “like” button for me.

For Yang Jian this warm male: really really very useful, your support will be I continue to write the article forward power, we see the next article.

“Original” | er lang shen Yang Jian

Original is not easy, don’t want to white piao. Erlang God Yang Jian, a programmer in the front of the Internet to scrape a living, focus on the front development, good at technology sharing. If you need to reprint, please contact the author or retain the original link, wechat public search Erlang God Yang Jian or scan the two-dimensional code below more convenient.

Come together to witness the growth of Erlang God Yang Jian! More good articles, technology to share in the public account below. Welcome to follow.