【问题标题】:Spring boot web appliaction not detecting the ControllerSpring Boot Web 应用程序未检测到控制器
【发布时间】:2020-03-22 11:39:15
【问题描述】:

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

  • spring-boot-starter-actuator
  • spring-boot-starter-web
  • spring-cloud-starter-config

Spring Boot 版本为 2.2.1

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

主类

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,即使这有帮助。

【问题讨论】:

  • RateController 缺少 package com.example.demo;
  • @ComponentScan(basePackages= {"com.example.demo"}) 是多余的,因为 @SpringBootApplication 是一个隐含的 @ComponentScan 使用注释类的包。如果要指定基础包,使用@SpringBootApplication(scanBasePackages = {"com.example.demo"}),不需要单独的@ComponentScan注解。
  • 没有帮助。我关闭了我的配置服务器并试图修改代码。出现错误 [引起:java.lang.IllegalArgumentException:无法解析值“${rate}”中的占位符“rate”]。所以我相信控制器正在被拾取,因为它正在尝试解析控制器的属性。

标签: java spring spring-boot thymeleaf


【解决方案1】:

知道了。是缺少的 Thmeleaf 依赖项造成了问题。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-25
    • 2016-08-29
    • 2018-03-25
    • 2019-07-16
    • 2019-11-11
    • 1970-01-01
    • 2019-01-07
    • 2018-07-28
    相关资源
    最近更新 更多