没有调用springboot restcontroller

时间:2017-07-19 23:00:49

标签: rest spring-boot

问题是我的休息控制器没有被调用。

@SpringBootApplication(scanBasePackages = { "com.company.base" })
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

@RestController
@RequestMapping("/data")
public class DataController {

    private static final Logger LOGGER = LogManager.getLogger(MethodHandles.lookup().lookupClass());

    @Autowired
    private SusbscriberRepository susbscriberRepository;

    @RequestMapping(value = "/subscribers", method = RequestMethod.GET)
    public HttpEntity<SubscriberResource> findAll() {
     ...
    }

    @RequestMapping(value = "/subscriber/{id}", method = RequestMethod.GET)
    public ResponseEntity<SubscriberResource> findSubscriber(@PathVariable(value = "id") Long id) {
      ...
    }
}


<?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.company.base</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Grenty</name>
    <description>Grenty project</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.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>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <organization>
        <name>SA Technologies</name>
        <url>www.satechnologies.com</url>
    </organization>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.5.4-Final</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version><!--$NO-MVN-MAN-VER$ -->
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
                <version>1.4.196</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>de.jpdigital</groupId>
                <artifactId>hibernate5-ddl-maven-plugin</artifactId>
                <version>1.0.1-hibernate-5.1.2.Final</version>
                <configuration>
                    <dialects>
                        <param>postgresql9</param>
                    </dialects>
                    <packages>
                        <param>com.company.base.model</param>
                    </packages>
                    <outputDirectory>${project.basedir}/docs/sql</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>gen-ddl</goal>
                        </goals>
                        <phase>process-classes</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

在Springboot开始日志中,映射了这些URL。

我确定我错过了一些东西,但我不知道是什么。

即使是春天的例子也不起作用:gs-actuator-service-master

4 个答案:

答案 0 :(得分:1)

有三件事可能有问题

  1. spring-boot-starter-tomcat&amp;缺少spring-boot-starter-web lib。
  2. 由于您已定义com.company.base
  3. ,您的控制器必须位于scanBasePackages包裹下
  4. Spring boot应用程序必须与SpringBootServletInitializer一起运行。只有在以Java应用程序运行时才需要这种情况,如果从mvn命令运行则不需要。
  5. 请添加图书馆&amp;更新您的Application.java,然后重试。

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(Application.class, args);
        }
    
    }
    

答案 1 :(得分:0)

如果控制器位于启动应用程序包或子包下,请删除scanBasePackages = {“com.company.base”}。 或者在组件扫描中包括主类的包。

答案 2 :(得分:0)

我遇到了类似的问题。 我添加了 spring-mvc 依赖项。

compile "org.springframework:spring-webmvc:5.1.3.RELEASE"

默认情况下,主类应位于根包中。我将主类放入另一个包中,因此必须进行组件扫描并添加其他可用组件。

package root.package1

@SpringBootApplication(scanBasePackages = {"root"})

公共类WebAppMain {

    public static void main(String[] args) {
        SpringApplication.run(WebAppMain.class, args);
    }
}

答案 3 :(得分:0)

将控制器类移动到应用程序类的子包中。

like 
    com.company.base for Application class
    com.company.base.controller for DataController class

然后,弹簧靴将自动扫描控制器。