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启动类:


UTF-8
UTF-8
1.8
<!-- 这里是我本人的springboot启动类位置,请根据自己的情况改动,idea下面可以点出来的-->
com.example.DemoApplication




org.springframework.boot
spring-boot-maven-plugin

true
${start-class}



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

你可能感兴趣的:(java,开发工具,后端)