【蠢事】Spring Boot项目启动访问页面报错Initializing Spring DispatcherServlet 'dispatcherServlet'

控制器代码如下所示:

import org.springframework.web.bind.annotation.GetMapping;

public class HelloController {
    @GetMapping(value="/hello")
    public Object hello() {
        return "Hello Mango!";
    }
}

启动器代码如下所示:

@SpringBootApplication
public class MangoAdminApplication {

    public static void main(String[] args) {
        SpringApplication.run(MangoAdminApplication.class, args);
    }
}

项目可正常启动不报错,但是访问hello页面时,报错内容如下所示

出错原因:

控制器类没有添加RestController注解

解决方法:

在控制器类中添加注解

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping(value="/hello")
    public Object hello() {
        return "Hello Mango!";
    }
}

成功运行

猜你喜欢

转载自www.cnblogs.com/guiyeku/p/11699133.html