SpringBoot1.5.9访问Controller显示404

报错信息如下
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 04 17:57:59 CST 2020
There was an unexpected error (type=Not Found, status=404).
No message available

解决

启动类要包扫描

@SpringBootApplication(scanBasePackages = "com.*") 

Controller类:

@Controller
@RequestMapping("/test")
public class HelloController {
	@ResponseBody
	@RequestMapping("/hello")
	public String hello() {
		System.out.println("ddd");
		return "hello world!";
	}
}

类的RequestMapping不能省略
在浏览器输入localhost:8080/test/hello就行了。

以上解决Controller类不在主配置类(@SpringBootApplication标注的类)所在的包下。
如果Controller类在主配置类所在的包下,(scanBasePackages = "com.") 和@RequestMapping("/test")都可以省略。*

原因是:
@SpringBootApplication中有个@EnableAutoConfiguration注解用于开启自动配置,在这个注解里面有个@AutoConfigurationPackage注解,功能如下在这里插入图片描述
如果没在主配置类包下,就得自己写扫描包的位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot 1.5.9中解决跨域问题可以通过实现WebMvcConfigurer接口来配置。首先,创建一个配置类并实现WebMvcConfigurer接口。然后,在该类中重写addCorsMappings方法,使用CorsRegistry对象来添加跨域配置。具体来说,可以通过调用CorsRegistry对象的addMapping方法来指定需要处理跨域的请求路径,并使用allowedOrigins、allowedMethods、allowedHeaders和allowCredentials方法来配置允许的源、方法、头部和凭据。 以下是一个示例配置类的代码片段,用于解决跨域问题: @Configuration public class CorsConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") // 指定需要处理跨域的请求路径,这里是所有路径 .allowedOrigins("*") // 允许所有源 .allowedMethods("*") // 允许所有方法 .allowedHeaders("*") // 允许所有头部 .allowCredentials(true); // 允许发送凭据信息 } } 这段代码中,addMapping("/**")表示将所有请求路径都进行跨域处理,allowedOrigins("*")表示允许所有源,allowedMethods("*")表示允许所有方法,allowedHeaders("*")表示允许所有头部,allowCredentials(true)表示允许发送凭据信息。 通过以上配置,SpringBoot 1.5.9可以成功解决跨域问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot教程(七) | SpringBoot解决跨域问题](https://blog.csdn.net/lsqingfeng/article/details/122614630)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [关于SpringBoot 项目跨域问题的几种解决方法](https://blog.csdn.net/2301_76607156/article/details/130485123)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值