热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringBootWeb应用程序未检测到控制器

我创建了一个具有以下依赖关系的基于SpringBoot的Web应用程序。spring-boot-starter-actuat

我创建了一个具有以下依赖关系的基于Spring Boot的Web应用程序。


  • spring-boot-starter-actuator

  • spring-boot-starter-web

  • spring-cloud-starter-config

春季启动版本为2.2.1


org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE


主班

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages= {"com.example.demo"})
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}

控制器如下所示

package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RefreshScope
@Controller
public class RateController {
@Value("${rate}")
private String rate;
@Value("${lanecount}")
private String laneCount;
@Value("${tollstart}")
private String tollStart;
@RequestMapping("/rate")
public String getRates(Model model) {
model.addAttribute("rate",rate);
model.addAttribute("lanecount",laneCount);
model.addAttribute("tollStart",tollStart);
return "rateView";
}
}

rateView.html在以下文件夹结构下可用

src/main/resources
|
templates
|
rateView.html

尝试访问URL [http://localhost:8080/rate]

时出现404错误

Whitelabel Error Page
This application has no explicit mapping for /error,so you are seeing this as a fallback.
Wed Nov 27 05:37:35 IST 2019
There was an unexpected error (type=Not Found,status=404).
No message available

我在控制台中也看不到任何日志,该日志显示“ / rate”已映射到“ RateController”

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__,| / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)
2019-11-27 05:37:03.612 INFO 10015 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2019-11-27 05:37:06.030 INFO 10015 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=s1rates,profiles=[default],label=null,version=f38d2a4b25fddb80e80b3f3c5ba096f5b6b41b03,state=null
2019-11-27 05:37:06.031 INFO 10015 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: OriginTrackedCompositePropertySource {name='configService',propertySources=[MapPropertySource {name='configClient'},OriginTrackedMapPropertySource {name='https://gitlab.com/exoneratetechnologies/tests/configserver.git/application.properties'}]}
2019-11-27 05:37:06.035 INFO 10015 --- [ main] c.example.demo.ConfigClientApplication : The following profiles are active: default
2019-11-27 05:37:06.497 INFO 10015 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=86d87992-3ac9-3a8f-ae23-29a44e535b31
2019-11-27 05:37:06.536 INFO 10015 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d97a3d06] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-27 05:37:06.653 INFO 10015 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-27 05:37:06.660 INFO 10015 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-27 05:37:06.660 INFO 10015 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-27 05:37:06.720 INFO 10015 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-27 05:37:06.720 INFO 10015 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 674 ms
2019-11-27 05:37:06.974 INFO 10015 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-27 05:37:07.128 INFO 10015 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-11-27 05:37:07.169 INFO 10015 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-27 05:37:07.174 INFO 10015 --- [ main] c.example.demo.ConfigClientApplication : Started ConfigClientApplication in 4.481 seconds (JVM running for 4.896)
2019-11-27 05:37:35.819 INFO 10015 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-11-27 05:37:35.820 INFO 10015 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-11-27 05:37:35.825 INFO 10015 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms

假定未检测到我的控制器类,我在主类中添加了@ComponentScan,即使这样也有所帮助。



知道了。是导致问题的Thmeleaf依赖项丢失。


org.springframework.boot
spring-boot-starter-thymeleaf



推荐阅读
author-avatar
hanhff
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有