无法使用Spring Boot自动配置访问REST控制器

时间:2016-10-30 07:35:29

标签: java rest spring-boot

我正在尝试使用this tutorial之后的Spring Boot创建一个简单的REST服务。来自webapp文件夹(index.html)的“Hello World”html文件正在http://localhost:8080/my-rest-app/上打开(我创建了一个Maven-Web-App,因为我希望为服务提供“欢迎页面”)。但是,如果我尝试在http://localhost:8080/my-rest-app/user上访问REST服务,我会收到404消息。

的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>service</groupId>
    <artifactId>my-rest-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>my-rest-app</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Application.java:

package service.my.rest.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
//@ComponentScan({"service.my.rest.app", "service.my.rest.app.controller"})
//@ComponentScan(basePackageClasses = UserController.class)
public class Application {

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

}

UserController.java:。

package service.my.rest.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping(method = RequestMethod.GET)
    public String getUser() {
        return "Hello";
    }

}

我错过了什么?服务的URL是错误的吗?我在某处读到Spring Boot REST服务的上下文路径始终为/,这意味着我必须通过http://localhost:8080/my-rest-app/访问该服务,但这不起作用(不index.html )。使用application.propertiesserver.contextPath=/my-rest-app更改server.port=8080中的上下文路径也无济于事。

2 个答案:

答案 0 :(得分:1)

问题在于构建和部署过程。谢谢@chrylis提示。

package service.my.rest.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"service.my.rest.app"})
public class Application extends SpringBootServletInitializer {

    private static Class<Application> application = Application.class;

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(application);
    }

}

我必须将我的应用扩展到build and deploy a war file,因为Spring Boot is actually designed to produce a jar

答案 1 :(得分:0)

如果您的控制器位于外部模块中,则可以通过填充文件来提供鞭子组件自动配置: src / main / resources / MATA_INF / sping.factories

如果您的控制器是:

public ResponseEntity<?> execCommand(ComandoStr msg) {
@RequestMapping(value = "/api/exec")
@RestController
public class ExecController {

@PostMapping(value = "command")
public ResponseEntity<?> execCommand(ComandoStr msg) {
   ...
}

}

在sping.factory中,您需要添加条目

imp.macchina.webapi.ExecController

然后控制器注册