springboot学习:3.第一个Spring Boot Application

(题外话:我写的这些博文,比起像学习手册来说,更像是我学习过程的再现,自己所踩过的坑,所以单个看会觉得很奇怪,如果有兴趣可以联系我的上下博文,谢谢。)
操作背景:java version “1.7.0_51”;Spring CLI v1.3.0.RELEASE;Apache Maven 3.3.9,并且测试运行成功了,可是还是遇到了本篇的问题。(因为不是安装官方配置的版本)
测试运行成功可了解:
springboot学习:1.安装
springboot学习:2.测试运行
下面开始Application:

①创建pom
选择一个目录后,创建如下pom.xml文件;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>myproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.11.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>
</project>

该pom声明了父pom文件,添加了spring-boot-starter-web依赖用来创建web项目。

②编写代码
在该目录下创建src/main/java文件夹,是为了模仿maven的目录排版习惯。
创建src/main/java/Example.java文件,如下:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
	@RequestMapping("/")
	String home() {
		return "Hello World!";
	}
	public static void main(String[] args) {
		SpringApplication.run(Example.class, args);
	}
}

@RestController标签属于springframework的标签,同@Controller一样,声明一个web控制器;
@RequestMapping标签用来拦截请求路径,当该标签内的请求出现时,则调用该标签声明的方法,即home( );
@EnableAutoConfiguration是自动装配标签,用来自动装配所需环境及上下文。
main( )方法用来告知SpringApplication的run( )方法来运行本例。

③运行本例
在该项目目录下执行mvn spring-boot:run命令,出现了如下报错:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project myproject: Fatal error compiling: 无效的标记: -parameters -> [Help 1]

因为解决过该问题,所以通过初学MAVEN之compile失败把这一步解决了。可是紧接着还是有了别的问题。问题大概如下:

[WARNING] Error injecting: org.springframework.boot.maven.RunMojo
java.lang.TypeNotPresentException: Type org.springframework.boot.maven.RunMojo not present
        at org.eclipse.sisu.space.URLClassSpace.loadClass(URLClassSpace.java:147)
        at org.eclipse.sisu.space.NamedClass.load(NamedClass.java:46)
        at org.eclipse.sisu.space.AbstractDeferredClass.get(AbstractDeferredClass.java:48)
        at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
        at com.google.inject.internal.InternalFactoryToInitializableAdapter.provision(InternalFactoryToInitializableAdapter.java:53)

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-
plugin:2.1.11.RELEASE:run (default-cli) on project myproject: Execution default-cli
 of goal org.springframework.boot:spring-boot-maven-plugin:2.1.11.RELEASE:run 
 failed: Unable to load the mojo 'run' in the plugin 
 'org.springframework.boot:spring-boot-maven-plugin:2.1.11.RELEASE' due to an API
  incompatibility: 
  org.codehaus.plexus.component.repository.exception.ComponentLookupException: 
  org/springframework/boot/maven/RunMojo : Unsupported major.minor version 52.0

看的头大,折腾了好久。怀疑自己因为偷懒,没有按官方提示的java8和springboot2来操作。无奈,只能重新开始,安装官方要求来操作。
重新配置如下:java version “1.8.0_231”,Spring CLI v2.1.11.RELEASE,Apache Maven 3.3.9。(解决java双版本可看解决java7/8共存问题
再按以上步骤可运行成功。
界面停留在如下:
在这里插入图片描述
此时访问http://localhost:8080/会出现“Hello World!”字样。
在这里插入图片描述

发布了13 篇原创文章 · 获赞 0 · 访问量 223

猜你喜欢

转载自blog.csdn.net/weixin_43859070/article/details/103901507