码迷,mamicode.com
首页 > 编程语言 > 详细

springboot启动后controller访问404

时间:2018-10-14 21:54:11      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:ica   figure   body   map   cat   boot   reporting   lease   framework   

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

    springboot启动后controller访问404

    标签:ica   figure   body   map   cat   boot   reporting   lease   framework   

    原文地址:http://blog.51cto.com/3440684/2299831

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!