Spring Boot Kotlin API为所有端点返回404响应

时间:2018-05-21 17:19:45

标签: java spring-boot kotlin

我使用Spring Boot和Kotlin。其余控制器为所有端点返回404错误。在起始日志中,控制器中的端点不存在。任何端点都返回相同的404响应。

这些是我尝试过的步骤,

  1. 尝试清除缓存/目标
  2. 将所有类移至单个目录
  3. 尝试添加ComponentScan注释但不接受。
  4. 我使用cmd行来运行项目
  5. 运行java -jar $ jarfile
  6. 时看到相同的输出
  7. 检查注释存储库,服务,RestController
  8. 控制器:

    package com.example.controller
    
    import org.springframework.web.bind.annotation.RestController
    import org.springframework.web.bind.annotation.PathVariable
    import org.springframework.http.ResponseEntity
    import org.springframework.web.bind.annotation.GetMapping
    import org.springframework.http.HttpStatus
    import org.springframework.web.bind.annotation.PostMapping
    import org.springframework.web.bind.annotation.PutMapping
    import org.springframework.web.bind.annotation.DeleteMapping
    import org.springframework.web.bind.annotation.RequestBody
    
    import Cricketer
    import CricketerRepository
    import CricketerService
    import org.springframework.web.bind.annotation.RequestMapping
    
    @RestController
    @RequestMapping("/api")
    class CricketerController(private val cricketerService: CricketerService , private val cricketerRepository: CricketerRepository){
    
        @GetMapping("/cricketers/{id}")
        fun getCricketer(@PathVariable("id") id: Long):ResponseEntity<Cricketer>  {
            val cricketer = cricketerService.findById(id)
            return ResponseEntity<Cricketer>(cricketer as Cricketer, HttpStatus.OK);
        }
    
        @GetMapping("/cricketers/")
        fun getAllCricketers() :ResponseEntity<List<Cricketer>>  {
            var cricketersList: ArrayList<Cricketer> =  cricketerService.getAllPlayers() as (ArrayList<Cricketer>)
            return ResponseEntity<List<Cricketer>>(cricketersList, HttpStatus.OK)
        }
    
        @PostMapping("/cricketer/")
        fun addCricketer(@RequestBody cricketer:Cricketer):ResponseEntity<Cricketer>  {
            val cCricketer : Cricketer = Cricketer(name = cricketer.name
                    , country = cricketer.country
                    , highestScore = cricketer.highestScore)
            cricketerRepository.save(cCricketer)
            return ResponseEntity<Cricketer>(cricketer , HttpStatus.OK)
        }
    
        @PutMapping("/cricketer/{id}")
        fun updateCricketer(@PathVariable("id") id: Long, @RequestBody cricketer: Cricketer ):ResponseEntity<Cricketer> {
            val cCricketer  = Cricketer(name = cricketer.name
                    , country = cricketer.country
                    , highestScore = cricketer.highestScore)
            cricketerRepository.save(cCricketer)
            return ResponseEntity<Cricketer>(cricketer, HttpStatus.OK)
        }
    
        @DeleteMapping("/cricketer/{id}")
        fun deleteCricketer(@PathVariable("id") id:Long ):ResponseEntity<String> {
            val cCricketer :Cricketer  = cricketerService.findById(id) as Cricketer
            cricketerRepository.delete(cCricketer)
            return ResponseEntity<String>("cricketer removed", HttpStatus.OK)
        }
    }
    

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>Spring-Kotlin-Rest-API</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>Spring-Kotlin-Rest-API</name>
        <description>Spring Boot Kotlin REST API Example</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <kotlin.version>1.2.41</kotlin.version>
    
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.module</groupId>
                <artifactId>jackson-module-kotlin</artifactId>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib-jdk8</artifactId>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-reflect</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
            <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>       
                </plugin>
                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <configuration>
                        <args>
                            <arg>-Xjsr305=strict</arg>
                        </args>
    
                        <compilerPlugins>
    
                            <plugin>spring</plugin>
                        </compilerPlugins>
                        <jvmTarget>1.8</jvmTarget>          
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.jetbrains.kotlin</groupId>
                            <artifactId>kotlin-maven-allopen</artifactId>
                            <version>${kotlin.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </project>
    

    启动日志

    2018-05-21 22:31:19.490  INFO 3061 --- [           main] c.e.d.SpringKotlinRestApiApplicationKt   : Starting SpringKotlinRestApiApplicationKt on nirmal-desktop with PID 3061 (/home/nirmal/code/workspace-sts-3.9.1.RELEASE/Spring-Kotlin-Rest-API/target/classes started by root in /home/nirmal/code/workspace-sts-3.9.1.RELEASE/Spring-Kotlin-Rest-API)
    2018-05-21 22:31:19.512  INFO 3061 --- [           main] c.e.d.SpringKotlinRestApiApplicationKt   : No active profile set, falling back to default profiles: default
    2018-05-21 22:31:19.592  INFO 3061 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5bab7c16: startup date [Mon May 21 22:31:19 IST 2018]; root of context hierarchy
    2018-05-21 22:31:29.492  INFO 3061 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$28d3b505] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2018-05-21 22:31:33.847  INFO 3061 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2018-05-21 22:31:34.312  INFO 3061 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2018-05-21 22:31:34.313  INFO 3061 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
    2018-05-21 22:31:34.389  INFO 3061 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
    2018-05-21 22:31:36.190  INFO 3061 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2018-05-21 22:31:36.190  INFO 3061 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 16602 ms
    2018-05-21 22:31:36.560  INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
    2018-05-21 22:31:36.564  INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2018-05-21 22:31:36.566  INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2018-05-21 22:31:36.566  INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2018-05-21 22:31:36.566  INFO 3061 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2018-05-21 22:31:37.743  INFO 3061 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
    2018-05-21 22:31:39.038  INFO 3061 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
    2018-05-21 22:31:39.222  INFO 3061 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
    2018-05-21 22:31:39.480  INFO 3061 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
    2018-05-21 22:31:40.064  INFO 3061 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.17.Final}
    2018-05-21 22:31:40.066  INFO 3061 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
    2018-05-21 22:31:40.302  INFO 3061 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    2018-05-21 22:31:42.180  INFO 3061 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    2018-05-21 22:31:44.235  INFO 3061 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@7d95eb4a'
    2018-05-21 22:31:44.237  INFO 3061 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
    2018-05-21 22:31:44.688  INFO 3061 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-21 22:31:46.829  INFO 3061 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5bab7c16: startup date [Mon May 21 22:31:19 IST 2018]; root of context hierarchy
    2018-05-21 22:31:46.894  WARN 3061 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : 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
    2018-05-21 22:31:47.072  INFO 3061 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    2018-05-21 22:31:47.073  INFO 3061 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2018-05-21 22:31:47.121  INFO 3061 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-21 22:31:47.122  INFO 3061 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-05-21 22:31:49.030  INFO 3061 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2018-05-21 22:31:49.033  INFO 3061 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
    2018-05-21 22:31:49.047  INFO 3061 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
    2018-05-21 22:31:50.682  INFO 3061 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2018-05-21 22:31:50.695  INFO 3061 --- [           main] c.e.d.SpringKotlinRestApiApplicationKt   : Started SpringKotlinRestApiApplicationKt in 34.697 seconds (JVM running for 49.043)
    

1 个答案:

答案 0 :(得分:1)

我猜你的CricketerController不在相对于你的主类的子包中。所以你基本上有两个选择:

  1. 将您的主要课程直接放在 com.example
  2. 包裹下
  3. 将此答案底部的注释添加到主班。
  4. 使用这两种方法中的任何一种方法都可以使Spring在启动时看到控制器类,因此应该创建映射。

    @ComponentScan(basePackages = { "com.example.controller"} )