无法到达控制器中的端点,尽管它已被初始化

Posted

技术标签:

【中文标题】无法到达控制器中的端点,尽管它已被初始化【英文标题】:Cant reach Endpoint in Controller, despite it beeing intialised 【发布时间】:2020-06-16 01:57:55 【问题描述】:

我读过this 和this 主要想法是有人有错误的结构并且组件没有被扫描,我有一个正确的。

我的控制器正在正常初始化。我测试了它调试并在构造函数上设置断点。它正在运行。尽管我的测试、邮递员和浏览器都无法到达端点。我得到一个404。 我正在使用gradle。像this 这样构建我的代码。已经花了 3 个小时试图解决这个问题,但没有成功。

我的控制器看起来像这样。

package com.fressnapf.microservices.orderhistory.controller.impl;

@RestController
@RequestMapping("/customer")
public class OrderHistoryController implements IOrderHistoryController 
    ...
    @Override
    @ResponseStatus(value = HttpStatus.OK)
    @RequestMapping(value = "/customerid/orders", method = RequestMethod.GET, produces = "application/json")
    public String getOrders(@PathVariable("customerid") String customerid, @RequestParam(required = false) String timeFrom,
                        @RequestParam(required = false) String timeTo, @RequestParam(required = false) String openOnly) 
        ...
    

应用类

package com.fressnapf.microservices.orderhistory;

@SpringBootApplication()
@ImportResource("classpath*:applicationContext.xml")
@Configuration()
public class Application 
public static void main(String[] args) 
    ApplicationContext ctx = SpringApplication.run(Application.class, args);


这是我收到的回复


"timestamp": "2020-03-03T16:00:33.489+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/customer/000000000/orders"

这是我在应用程序中获得的日志

    2020-03-03 16:59:27.595  INFO 10406 --- [           main] c.f.m.orderhistory.Application           : Starting Application on debian-sgtechedge with PID 10406 (/home/sergeygerodes/projects/scporderhistoryservice/build/classes/java/main started by sgerodes in /home/sergeygerodes/projects/scporderhistoryservice)
    2020-03-03 16:59:27.602  INFO 10406 --- [           main] c.f.m.orderhistory.Application           : No active profile set, falling back to default profiles: default
    2020-03-03 16:59:28.414  INFO 10406 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
    2020-03-03 16:59:28.441  INFO 10406 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 16ms. Found 0 JPA repository interfaces.
    2020-03-03 16:59:28.768  INFO 10406 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2020-03-03 16:59:29.035  INFO 10406 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2020-03-03 16:59:29.046  INFO 10406 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-03-03 16:59:29.047  INFO 10406 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
    2020-03-03 16:59:29.140  INFO 10406 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-03-03 16:59:29.140  INFO 10406 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1475 ms
    2020-03-03 16:59:29.294  INFO 10406 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
    2020-03-03 16:59:29.405  INFO 10406 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
    2020-03-03 16:59:29.424  INFO 10406 --- [           main] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:orderhistory'
    2020-03-03 16:59:29.517  INFO 10406 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
    2020-03-03 16:59:29.565  INFO 10406 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core 5.4.9.Final
    2020-03-03 16:59:29.660  INFO 10406 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.1.0.Final
    2020-03-03 16:59:29.741  INFO 10406 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
    2020-03-03 16:59:29.886  INFO 10406 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
    2020-03-03 16:59:29.892  INFO 10406 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
    2020-03-03 17:00:28.614  WARN 10406 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Thread starvation or clock leap detected (housekeeper delta=59s108ms538?s586ns).
    2020-03-03 17:00:28.685  WARN 10406 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
    2020-03-03 17:00:28.845  INFO 10406 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2020-03-03 17:00:29.052  INFO 10406 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-03-03 17:00:29.060  INFO 10406 --- [           main] c.f.m.orderhistory.Application           : Started Application in 61.937 seconds (JVM running for 62.493)
    2020-03-03 17:00:33.395  INFO 10406 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
    2020-03-03 17:00:33.395  INFO 10406 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
    2020-03-03 17:00:33.404  INFO 10406 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 9 ms

整个 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="loggerService"
          class="com.fressnapf.sdk.logger.service.impl.DefaultLogService" />

    <bean id="dataService"
          class="com.fressnapf.sdk.dataaccess.services.impl.H2DataProvider">
        <constructor-arg index="0" value="$spring.datasource.url"/>
        <constructor-arg index="1" value="$spring.datasource.username"/>
        <constructor-arg index="2" value="$spring.datasource.password"/>
    </bean>

</beans>

【问题讨论】:

你在尝试什么端点?看起来你在 Linux 中;您可以尝试(在应用程序运行的情况下)curl -X GET -H "Accept: application/json" http://localhost:8080/customer/10000001/orders 并粘贴该命令的结果吗? @x80486 是的,我在 linux 上。 curl 的响应是 "timestamp":"2020-03-03T16:20:17.520+0000","status":404,"error":"Not Found","message":"No message available","路径":"/customer/10000001/orders" 这与我发送的请求基本相同。您可以在问题的“回复”部分看到它。 这很奇怪,它应该可以工作,除非 URL 中有其他内容,我在启动消息中看不到...applicationContext.xml 的内容是什么?顺便说一句,Application 中的@Configuration 什么都不做……你可以删除它。还将您的application.y[a]ml 粘贴到 ../main/resources/` 中。 @x80486 我的项目中没有 application.yamls... 你在哪里看到它们?粘贴在我的 applicationContext 的描述中 【参考方案1】:

如果您复制了控制器代码并将其粘贴到此处... 右括号太多了。我在我的一个控制器上对其进行了测试,它显示了相同的行为。

@RequestMapping(value = "/customerid/orders--><--"...

【讨论】:

天哪! -_- 。对我来说最尴尬的事情。非常感谢。 是的,其中一个错别字:D。也许使用 spring-boot-actuator 暴露端点以显示所有端点。

以上是关于无法到达控制器中的端点,尽管它已被初始化的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL 更新触发器 - 无法更新存储函数/触发器中的表,因为它已被语句使用

无法更新存储函数/触发器中的表“事务”,因为它已被调用此存储函数/触发器的语句使用

错误代码:1442。无法更新存储函数/触发器中的表“客户”,因为它已被调用此存储函数的语句使用

属性在模板中不存在,尽管它已分配

MySql 错误:无法更新存储函数/触发器中的表,因为它已被调用此存储函数/触发器的语句使用

无法更新存储函数/触发器中的表“example_table”,因为它已被调用此存储函数/触发器的语句使用