1.需求
浏览器发送hello请求,服务器接受请求并处理,响应hello world字符串;
2.创建maven工程
这样做的目的是: 让其后创建的Project 都是默认的使用自己的Maven
导入自己的Maven配置
Maven的具体配置和下载详情请看:https://blog.csdn.net/Yuz_99/article/details/88927253
打开IDEA - 》 new - 》 project - 》 选择Maven - 》 选择好JDK -》 next - 》进入页面后选择 Enable Auto-Import(自动导入 在pom.xml文件里面写一个依赖 IDEA会自动导入依赖)
2.导入spring -boot 相关的依赖
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.编写主程序:启动Spring Boot应用
在src - 》main-》java 下 创建一个类Java.class 名字为MainApplication
在主类上方写上,@SpringApplication (来标注这是一个主程序类,说明这是一个Spring Boot应用)
然后输入psvm
在里面写入
SpringApplication.run(类名.class,args);
(代表Spring应用启动起来)
此类为:SpringApplication.run(MainApplication.class,args);
4.编写相关的Controller,Service
在main.java.com.study下创建Java.class 名为 controller.helloController(意味着study下还有一个子包 包名controller 此包底下还有一个类 类名为helloController)
切记:Application 需要run的类要包含子类Controller
在上方下入 @Controller (处理请求)
浏览器会发送hello请求 标识一下@RequestMapping
把return的内容 写出去 把hello world写到浏览器 需要结合一个注解@ResponseBody
总体效果图为:
验证完成
如果报错:This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Mar 31 14:49:15 CST 2019 There was an unexpected error (type=Not Found, status=404). No message available
详情请看:https://blog.csdn.net/weixin_42205494/article/details/81330615 此博主内容!