手把手教你搭建SpringBoot项目

目录

概述

构建项目

项目运行

总结


最近在研究thingsboard这个项目,采用的是SpringBoot框架,作为一个APP的开发者,需要先了解下这个框架,然后再去研究thingsboard这个项目。所以简单的总结下自己在搭建SpringBoot项目时遇到的问题

概述

SpringBoot基于Spring Framework,用来快速搭建Spring应用。SpringBoot整合很多框架,所以说是库的集合。相对于Maven来说,Maven是jar包的集合。

SpringBoot设计的目的就是能够尽快的让Spring应用程序跑起来,并且减少配置文件。

SpringCloud是构建SpringBoot的分布式环境,也就是通常说的云应用。

而Spring应用程序有三层体系架构:web、service、dao。

构建项目

前提已经装好JDK 1.8以上、管理工具Maven、开发工具Intellij IDEA

  • 1.使用Spring Assistant新建项目,点击File->New->Project...,会弹出如下对话框

如果没有装插件在Intellij是无法Spring Assistant选项的,所以需要在Plugins搜索Spring Assistant,然后安装。我截图显示的"Uninstall"是因为自己已经安装了该插件。

有了这个插件,就会重复第一步新建工程的时候出现Spring Assistant选项。在第一步的操作中要选择一个默认的url。其中https://start.spring.io这个地址有时候会抛出“connect timeout”,多试几次就可以成功,当然也可以在Custom设置为“https://start.aliyun.com”。点击next就进入了下一步

  • 2.填写项目信息

这里注意一点Project name字母要用小写,否则在后面生成无法启动类SpringbootApplication,只会生成一个Application类。

  • 3.勾选web依赖和SQL依赖

因为thingsboard采用的是PostgreSQL,所以在选择的时候,我感觉应该是要选择PostgreSQL Driver

  • 4.选择项目路径进行保存即可。

项目运行

在项目中增加请求HelloSpringBootController来进行测试运行项目,其中HelloSpringBootController类中的代码如下:

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;
    }

}

其中启动类SpringbootApplication的代码如下,我们运行该类。

 

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);
	}

}

然后在 Intellij的输出显示如下,说明SpringbootApplication启动成功。

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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)

当然有时候在运行的过程中会出现8080端口被占用,找到占用8080端口的程序,然后杀掉该程序。

<!--找到所有的占用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$

在浏览器中输入“http://localhost:8080/hello”,界面显示如下:

输入“http://localhost:8080/helloint”,界面显示如下:

总结

一个简单的SpringBoot项目可以运行成功了,那么借鉴这个简单的例子,我可以了解到在去看thingsboard项目的时候,应该可以从org.thingsboard.server.controller下面的controller进行入手。但是对于SpringBoot框架自己还需要了解的内容:

(1)注解的含义以及可以实现的功能

(2)web页面与这些注解是怎么渲染出页面的

(3)怎么来操作数据库的

目前想到这些问题,继续加油吧。

猜你喜欢

转载自blog.csdn.net/nihaomabmt/article/details/107513389