龙空技术网

SpringBoot第一个程序(基于idea2021)

程序员技术提升营 188

前言:

当前姐妹们对“2021版的idea里面没有spring”大概比较珍视,大家都需要剖析一些“2021版的idea里面没有spring”的相关内容。那么小编在网摘上汇集了一些关于“2021版的idea里面没有spring””的相关内容,希望你们能喜欢,大家快快来了解一下吧!

一、使用Idea2021新建工程新建工程使用Spring Initializr创建spring boot工程,按图设置工程属性选择最新SpringBoot稳定版本2.6.1,并添加web支持等待jar包的下载对应的pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="; xmlns:xsi=";xsi:schemaLocation=" ;><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.1</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo1</artifactId><version>0.0.1-SNAPSHOT</version><name>demo1</name><description>demo1</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
二、换源注意:start.spring.io这个网站可能访问不了这里可以换用阿里镜像需要注意的是使用阿里镜像,可能选择不了最新的SpringBoot版本对应的pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="; xmlns:xsi=";xsi:schemaLocation=" ;>	<modelVersion>4.0.0</modelVersion>	<groupId>com.example</groupId>	<artifactId>SprngBoot1</artifactId>	<version>0.0.1-SNAPSHOT</version>	<name>SprngBoot1</name>	<description>SprngBoot1</description>	<properties>		<java.version>1.8</java.version>		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>		<spring-boot.version>2.4.1</spring-boot.version>	</properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.4.1</version><configuration><mainClass>com.example.sprngboot1.SprngBoot1Application</mainClass></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

如果需要用到最新版本的springboot,可以用2.6.1版本的pom将其替换掉

三、运行测试打开main函数,运行Tomcat运行成功新建包Controller,添加HelloControllerHelloController源码

package com.example.demo1.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {	@GetMapping("/")	public String hello(){		return "Hello World!";	}}
打开浏览器,输入:

标签: #2021版的idea里面没有spring