控制器代码如下所示:
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!"; } }
成功运行