springboot启动后controller访问404
-
首先需要在springboot的启动类上面使用@SpringBootApplication注解,并且指定扫描的包的位置,如下:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(scanBasePackages="com.example.controller")
public class DemoApplication {public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
}
这里如过需要扫描多个包可以这么写scanBasePackages={"com.xxx","com.xxx"}这种形式即可
2.其次在当前的pom.xml中指定springboot启动类:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 这里是我本人的springboot启动类位置,请根据自己的情况改动,idea下面可以点出来的-->
<start-class>com.example.DemoApplication</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</build>
3.这里是我的controller:
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/home") public class TestController { @RequestMapping("/hello") @ResponseBody public String index(){ return "hello world"; } }
注意:以上的springboot版本是2.0.5.RELEASE版,不同版本可能会有所不同。
springboot启动后浏览器输入下面的URL即可
http://localhost:8080/home/hello
- spring boot 正常启动,访问控制时报404
- Spring Boot项目访问controller方法报404
- springboot踩坑记--springboot正常启动但访问404
- spring boot成功启动后访问报错404的问题
- spring boot成功启动后访问报错404的问题
- spring boot成功启动后访问报错404的问题
- springboot 在idea多模块下 子模块的web项目用内置tomcat启动访问jsp报404
- Spring boot 启动jsp访问404问题解决
- springboot启动正常,访问restController报404
- spring boot成功启动后访问报错404的问题
- Spring Boot Controller 无法访问 type=Not Found, status=404
- 解决:sprng boot 项目在IDEA中spring-boot run正常运行,但是打成JAR包之后访问界面返回404
- 大家来找茬-SpringMVC中Tomcat正常启动,始终访问不了Controller,出404错
- IntelliJ IDEA+SpringBoot中静态资源访问路径陷阱:静态资源访问404
- 解决Spring Boot 正常启动后访问Controller提示404问题
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
- SpringBoot启动后为何无法正常访问接口
- springboot项目部署JBOSS成功但访问404
- springboot项目访问不到controller方法。
- 访问不存在的Thymeleaf 资源时使页面转向 404,而不是500 (Spring Boot)