springboot启动后controller访问404

  1. 首先需要在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

转载于:https://blog.51cto.com/3440684/2299831

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值