一、创建目录
1.点击Create New Project选择SPring Initializr并点击Next。
**2.填写Java项目结构名,并选择安装的Java版本(这里需选择8),然后点击Next。
3.选择Web项目中的Spring Web并Next后Finish。
4.项目创建完成:
二、项目运行
1.在com.example.webdemo主程序包下新建一个类HelloController,编写代码:
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController
{
@GetMapping("/say")
public String sayHello()
{
return "helloword Spring Boot!这是一个用Spring Boot开发的网站。";
}
}
2.然后会自动生成一个WebdemoApplication,点击运行WebdemoApplication,会出现:
说明启动成功
3.打开任意的浏览器输入http://localhost:8080/hello/say
!出现报错,经搜索发现可能存在跳转页面的url无对应的值的问题,
① Application启动类的位置不对.要将Application类放在最外侧,即包含所有子包 原因:spring-boot会自动加载启动类所在包下及其子包下的所有组件.
②在springboot的配置文件:application.yml或application.properties中关于视图解析器的配置问题: 当pom文件下的spring-boot-starter-paren版本高时使用: spring.mvc.view.prefix/spring.mvc.view.suffix 当pom文件下的spring-boot-starter-paren版本低时使用: spring.view.prefix/spring.view.suffix
③控制器的URL路径书写问题
@RequestMapping(“xxxxxxxxxxxxxx”)
实际访问的路径与”xxx”不符合.
(转载自博主“渴望飞的鱼”详情见https://blog.youkuaiyun.com/qq_36411874/article/details/93486993)
解决问题后刷新页面