一、实现的效果
在浏览器地址栏输入http://localhost:8080/hello,当前页面显示hello world
实例一代码:点击查看LearnSpringBoot01
点击查看更多的SpringBoot教程
二、 效果图
三、 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<groupId>org.example</groupId>
<artifactId>LearnSpringBoot01</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>15</java.version>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--
Spring Boot插件,将应用打包成一个可执行的jar包
原文链接:https://blog.csdn.net/qheuwq/article/details/130953123
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、HelloController.java代码
package org.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//这个类所有方法返回的数据直接写给浏览器,(如果是对象转换为json数据)
//@ResponseBody
//@Controller
@RestController // 等于上面 ResponseBody 加 Controller
public class HelloController {
// @ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
}
五、HelloWorldMainApplication.java代码
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
/**
* https://www.bilibili.com/video/av59572480/?p=5&spm_id_from=pageDriver&vd_source=4110e082785c06a8c24a5c86c6182472
*
* SpringBootApplication 来标注一个主程序类,说明这是一个spring boot应用
*
* SpringBoot出现找不到或无法加载主类解决办法 : 点击项目的目录,鼠标右键选择Maven->Reload Project,或者进行mvn clean后重新运行
* https://blog.csdn.net/github_38924695/article/details/128236420
*/
//@RestController
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring 来启动应用
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}