代码之家  ›  专栏  ›  技术社区  ›  Kaigo

从未使用Springboot@RestController

  •  5
  • Kaigo  · 技术社区  · 8 年前

    我的spring boot应用程序无法正常工作。目前只暴露了一个端点。它只有一个控制器来管理它。下面是代码。

    它构建良好,tomcat服务器运行正常,我得到 白标错误页 .

    chrome控制台出现完全错误: 加载资源失败:服务器响应状态为404。

    src
    --|main
    ----|java
    ------|com.domain.myproject.api
    --------|Controller             <----(this is a package in intellij)
    ----------|RestController.java
    --------|Springboot             <----(this is a package in intellij)
    ----------|SpringApplication.java
    

    RESTController。Java语言

    package com.example.myproject.api.Controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class RESTController {
    
        @GetMapping("/view/{id}")
        public String hello (@PathVariable("id") String id){
            return "return" + id;
        }
    
    }
    

    SpringApplication。Java语言

    package com.example.myproject.api.Springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class CustomsApiApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CustomsApiApplication.class, args);
        }
    }
    

    pom xml

    这些是依赖项,我将使用它们。

    <dependencies>
            <!--SpringBoot Dependencies-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>1.5.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <!-- Camel dependencies -->
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-core</artifactId>
                <version>2.20.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.camel</groupId>
                <artifactId>camel-jms</artifactId>
                <version>2.20.1</version>
            </dependency>
    
            <!-- ActiveMQ dependencies -->
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-broker</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-camel</artifactId>
            </dependency>
        </dependencies>
    

    谢谢

    另请进入intellij run控制台。

    2018-01-23 18:28:14.879 ERROR 10541 --- [nio-8080-exec-1] 
    o.s.d.r.w.RepositoryRestExceptionHandler : Could not resolve repository 
    metadata for 1234.
    
    2 回复  |  直到 8 年前
        1
  •  7
  •   Luay Abdulraheem    8 年前

    根据Spring boot文档 14.2 Locating the main application class ,建议您将主应用程序类定位在根包中的其他类之上。

    这个 @EnableAutoConfiguration 注释通常放在主类上,它隐式地定义了某些项目的基本搜索包。使用根包还允许 @ComponentScan 无需指定basePackage属性即可使用的注释。这两个注释都在中定义 @SpringBootApplication .

        2
  •  1
  •   musa mthembu    6 年前

    似乎您的问题在于包的结构方式,我建议您将CustomsApiApplication类从当前包中删除到应该是com的基本包中。实例我的项目

    link 真的帮了我