Teach you how to build a SpringBoot project

table of Contents

Overview

Build the project

Project run

to sum up


Recently I am studying the thingsboard project, using the SpringBoot framework. As an APP developer, you need to understand this framework first, and then study the thingsboard project. So simply summarize the problems I encountered when building the SpringBoot project

Overview

SpringBoot is based on Spring Framework and is used to quickly build Spring applications. SpringBoot integrates many frameworks, so it is a collection of libraries. Compared with Maven, Maven is a collection of jar packages.

The purpose of SpringBoot design is to make Spring applications run as quickly as possible and reduce configuration files.

SpringCloud is a distributed environment for building SpringBoot, which is commonly referred to as cloud applications.

The Spring application has a three-tier architecture: web, service, and dao.

Build the project

The premise is that JDK 1.8 or above, management tool Maven, development tool Intellij IDEA have been installed

  • 1. Use Spring Assistant to create a new project, click File->New->Project..., the following dialog box will pop up

If there is no plug-in installed, the Spring Assistant option is not available in Intellij, so you need to search for Spring Assistant in Plugins, and then install it. The "Uninstall" shown in my screenshot is because I have installed the plug-in.

With this plug-in, the Spring Assistant option appears when you repeat the first step to create a new project. Choose a default url in the first step of the operation. Among them, the address https://start.spring.io sometimes throws "connect timeout", which can be successful after a few tries. Of course, it can also be set to " https://start.aliyun.com " in Custom . Click next to enter the next step

  • 2. Fill in the project information

Note here that the letters of the Project name should be lowercase, otherwise the SpringbootApplication class that cannot be started will be generated later, and only an Application class will be generated.

  • 3. Check web dependency and SQL dependency

Because thingsboard uses PostgreSQL, when choosing, I feel that I should choose PostgreSQL Driver

  • 4. Select the project path to save.

Project run

Add a request for HelloSpringBootController to the project to test and run the project. The code in the HelloSpringBootController class is as follows:

package com.example.springboot.controller;

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


@RestController
public class HelloSpringBootController {
    @RequestMapping("/hello")
    public String hello() {
        return "Hello Spring Boot";
    }

    @RequestMapping("/helloint")
    public int helloInt() {
        return 123;
    }

}

The code of the startup class SpringbootApplication is as follows, we run this class.

 

package com.example.springboot;

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

@SpringBootApplication
public class SpringbootApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootApplication.class, args);
	}

}

Then the output of Intellij is displayed as follows, indicating that SpringbootApplication started successfully.

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-07-22 16:08:11.006  INFO 7592 --- [           main] c.e.springboot.SpringbootApplication     : Starting SpringbootApplication on liuwenjingMacBook-Pro.local with PID 7592 (/Users/j1/Documents/iOT/springboot/target/classes started by j1 in /Users/j1/Documents/iOT/springboot)
2020-07-22 16:08:11.008  INFO 7592 --- [           main] c.e.springboot.SpringbootApplication     : No active profile set, falling back to default profiles: default
2020-07-22 16:08:11.976  INFO 7592 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-07-22 16:08:11.988  INFO 7592 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-07-22 16:08:11.988  INFO 7592 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-22 16:08:12.064  INFO 7592 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-07-22 16:08:12.064  INFO 7592 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1003 ms
2020-07-22 16:08:12.209  INFO 7592 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-07-22 16:08:12.385  INFO 7592 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-07-22 16:08:12.395  INFO 7592 --- [           main] c.e.springboot.SpringbootApplication     : Started SpringbootApplication in 1.994 seconds (JVM running for 2.376)

Of course, sometimes port 8080 will be occupied during operation, find the program that occupies port 8080, and then kill the program.

<!--找到所有的占用8080端口的程序-->
MacBook-Pro:file xxx$ lsof -i:8080
COMMAND   PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
WeChat    258   j1   30u  IPv6 0x3f60708f15ffb7b9      0t0  TCP 172.16.20.40:49184->116.128.133.100:http-alt (ESTABLISHED)
?\x81?  290   j1   16u  IPv4 0x3f60708f203ec639      0t0  TCP 172.16.20.40:50287->157.255.245.177:http-alt (ESTABLISHED)
?\x81?  290   j1   31u  IPv4 0x3f60708f379ff339      0t0  TCP 172.16.20.40:50417->reverse.gdsz.cncnet.net:http-alt (ESTABLISHED)
QQ        295   j1   18u  IPv4 0x3f60708f1c3bc939      0t0  TCP 172.16.20.40:49204->153.3.50.86:http-alt (ESTABLISHED)
QQ        295   j1   21u  IPv4 0x3f60708f1c3bc939      0t0  TCP 172.16.20.40:49204->153.3.50.86:http-alt (ESTABLISHED)
<!--将占用8080端口的程序杀掉-->
MacBook-Pro:file xxx$ kill 290
<!--找到所有的占用8080端口的程序-->
MacBook-Pro:file xxx$ lsof -i:8080
COMMAND PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
WeChat  258   j1   30u  IPv6 0x3f60708f15ffb7b9      0t0  TCP 172.16.20.40:49184->116.128.133.100:http-alt (ESTABLISHED)
QQ      295   j1   18u  IPv4 0x3f60708f1c3bc939      0t0  TCP 172.16.20.40:49204->153.3.50.86:http-alt (ESTABLISHED)
QQ      295   j1   21u  IPv4 0x3f60708f1c3bc939      0t0  TCP 172.16.20.40:49204->153.3.50.86:http-alt (ESTABLISHED)
MacBook-Pro:file xxx$

Enter " http://localhost:8080/hello " in the browser, and the interface displays as follows:

Enter " http://localhost:8080/helloint ", the interface displays as follows:

to sum up

A simple SpringBoot project can run successfully, so by learning from this simple example, I can understand that when looking at the thingsboard project, I should be able to start from the controller under org.thingsboard.server.controller. But what you need to know about the SpringBoot framework itself:

(1) The meaning of annotations and the functions that can be realized

(2) How the web page and these annotations are rendered out of the page

(3) How to operate the database

Thinking of these problems now, keep on going.

Guess you like

Origin blog.csdn.net/nihaomabmt/article/details/107513389