springboot-基础步骤

自作多情 提交于 2020-01-29 04:48:35

一,介绍

1.1,什么是springBoot
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。换句话说,就是spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架

二,springBoot运行

进入http://start.spring.io/中下载springBoot的maven工程,用eclipse导项目或者idea生成springBoot项目
2.1,在pom.xml添加web依赖,提供web能力

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

2,2,编写controller
@RestController表示该controller的方法都以json格式返回
2,3,启动controller
2,4,访问http://localhost:8080/hello

三,打jar包

3.1添加打包插件

 <plugin>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.example.springboot_demo.SpringbootDemoApplication</mainClass>
    </configuration>
 </plugin>

3.2启动类继承SpringBootServletInitializer,重写configure方法
@Override//为了打包springboot项目
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}

四, 问题

4.1关于Idea中右边的maven projects窗口找不到了如何调出来
方法1.你点击一下你idea界面最左下角的那个小框,maven应该从里面找到(这个有待亲测)
方法2.点击菜单栏View->Tool Windows->Maven projects
方法3.点击菜单栏Help->Find Action(Ctrl+Shift+A),输入Maven projects
方法4.点击工具栏view ,选中 tool buttons -> Maven projects 即可
4.2打包运行
窗口命令,所在目录java -jar xxxx.jar
4.3使用命令打包
4.3.1命令行切换到项目所在磁盘的根目录
例如:F:\eclipse_workspace\spring_boot_project
4.3.2.执行命令:
mvn clean package -Dmaven.test.skip=true
其中-Dmaven.test.skip=true为跳过单元测试,不执行测试用例,也不编译测试用例类
注意:命令打包的时候可能会报`
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project activiti-bpmn-model: Fatal error compiling: 无效的标记: --release -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn -rf :activiti-bpmn-model

原因:maven的使用本机环境变量%JAVA_HOME% 中配置的jdk版本在1.8以下 修改一下版本即可
4.4打成war 包,把jar改成war
4.4.1在<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
添加成如下 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--打成war 移除依赖中的tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
需要添加一个依赖如下:

<!--tomcat是外部提供的-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>

4.4.2配置项目名,在build里插件下添加finalName标签可以配置项目名,要不就用默认的项目名称。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!