栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

应用程序没有针对/ error的显式映射

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

应用程序没有针对/ error的显式映射

当我们创建一个Spring Boot应用程序时,我们用注解对其进行

@SpringBootApplication
注解。该批注“包装”了许多其他必需的批注,以使应用程序正常工作。一种这样的注释是
@ComponentScan
注释。该注释告诉Spring寻找Spring组件并配置要运行的应用程序。

你的应用程序类必须位于程序包层次结构的顶部,以便Spring可以扫描子程序包并找到其他必需的组件。

package com.test.spring.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class App {    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }}

下面的代码片段适用于控制器程序包在com.test.spring.boot程序包下的情况

package com.test.spring.boot.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HomeController {    @RequestMapping("/")    public String home(){        return "Hello World!";    }}

以下代码段 不起作用,因为控制器软件包不在com.test.spring.boot软件包中

package com.test.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HomeController {     @RequestMapping("/")     public String home(){         return "Hello World!";     } }

从Spring Boot文档中:

许多sspring引导开发者总是有其主类注解为

@Configuration
@EnableAutoConfiguration
@ComponentScan
。由于这些注释经常一起使用(特别是如果你遵循上述最佳实践),因此Spring Boot提供了一种方便的
@SpringBootApplication
替代方法。

@SpringBootApplication
注解相当于使用
@Configuration
@EnableAutoConfiguration
@ComponentScan
与他们的默认属性



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369910.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号