使用IDEA新建Spring Boot项目
作者:互联网
IDEA可以通过两种方式创建Spring Boot 项目
- 使用Maven创建
- 使用Spring Initializr创建
本文采用Spring Initializr创建
1.新建项目
1.1 新建一个Spring Initializr项目,一般都是默认的,点击next
1.2 输入相关参数,点击Finish
1.3 Web下勾选Spring Web,选择适合的版本,点击next
1.4 根据需要修改项目名称及项目存储位置等信息,点击Finish
1.5 完成的Spring Boot项目的结构如下所示
1.6 配置application.properties文件
配置端口号(默认是8080)和项目名称,当然不配置也可以正常启动,只不过为了区别于其他项目,还是配置比较好
application文件有两种文件格式,一种是以.properties为后缀,一种是以.yml为后缀的,两种配置方式略有差别,详情可参考这个网址:https://blog.csdn.net/qq_29648651/article/details/78503853;
1.7 测试接口访问
创建controller目录,用来存放控制层文件,创建TestController类,添加@Controller注解,声明这是控制层,定义测试接口方法hello,打印"Hello springboot"
@Controller
@RequestMapping("test")
public class TestController {
@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello Springboot!";
}
}
1.8 启动类Application
可以看到配置文件生效
1.9 在浏览器中访问http://localhost:5012/myspringboot/test/hello
注意
TestController中的代码还可以写成
@RestController
@RequestMapping("test")
public class TestController {
@RequestMapping("/hello")
public String hello() {
return "Hello Springboot!";
}
}
@RestController是@Controller和@ResponseBody的结合体,两个标注合并起来的作用。
具体可以见博客https://blog.csdn.net/weixin_46099269/article/details/108274477和https://www.cnblogs.com/ming-zhi/p/9522238.html#:~:text=RestControll%E5%92%8CController%E7%9A%84%E5%8C%BA%E5%88%AB%20RestController%20%3D%20Controller,%2B%20ResponseBody%EF%BC%8C%E4%BE%8B%E5%A6%82%E5%9C%A8%E9%A1%B9%E7%9B%AE%E5%BD%93%E4%B8%AD%EF%BC%8C%E4%BD%A0%E5%8A%A0%E4%B8%8A%E7%9A%84%E6%98%AFRestController%2C%E9%82%A3%E4%B9%88%E8%BF%94%E5%9B%9E%E7%9A%84%E5%86%85%E5%AE%B9%E6%98%AF%E4%BD%A0return%E4%B8%AD%E7%9A%84%E5%86%85%E5%AE%B9%EF%BC%8C%E5%A6%82%E6%9E%9C%E6%98%AFreturn%20%22Hello%20World%22%EF%BC%8C%E9%A1%B5%E9%9D%A2%E6%98%BE%E7%A4%BA%E7%9A%84%E5%B0%B1%E6%98%AFHello%20World%E3%80%82
2.项目打包成jar包
2.1 点击IDEA右侧的Maven按钮,再点击m符号
2.2 输入命令mvn package
2.3 检查项目所在位置,看是否出现jar文件
2.4 使用命令行运行项目
首先进入到具体jar目录下,然后启动项目:java -jar 包名称 &
2.5 在浏览器中输入url测试
2.6 打包时遇到的问题
"D:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-Dmaven.multiModuleProjectDirectory=E:\Idea project\myspringboot" "-Dmaven.home=D:\Program Files\maven\apache-maven-3.5.4" "-Dclassworlds.conf=D:\Program Files\maven\apache-maven-3.5.4\bin\m2.conf" "-Dmaven.ext.class.path=D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=52429:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\maven\apache-maven-3.5.4\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version2019.3.3 -s "D:\Program Files\maven\apache-maven-3.5.4\conf\settings.xml" package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.example:myspringboot >----------------------
[INFO] Building myspringboot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ myspringboot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.299 s
[INFO] Finished at: 2022-07-23T22:08:05+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources) on project myspringboot: Input length = 1 -> [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
解决方案
修改maven-resources-plugin版本即可,将3.2.0改为3.1.0
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<!--修改版本-->
<version>3.1.0</version>
</plugin>
</plugins>
</build>
参考博客
https://blog.csdn.net/wjhgaodandan/article/details/110443668
标签:INFO,E5%,Spring,Boot,IDEA,jar,maven,Program,resources 来源: https://www.cnblogs.com/ycylikestudy/p/16513233.html