在使用Spring Boot在独立的tomcat上开发我的REST应用程序时获得404

时间:2017-05-31 04:36:43

标签: java spring spring-mvc tomcat spring-boot

我无法在独立的tomcat上使用Spring Boot和Java 8开发我的REST api。当我通过浏览器点击它时获得404。但是,当通过嵌入式tomcat命中时,同样的应用程序可以工作。

的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.study.rest</groupId>
    <artifactId>hello-world-rest-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>hello-world-rest-demo</name>
    <description>Hello World REST API - Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 
        To make the application both executable with embedded tc and also deployable on standalone tc. 
        Make sure to change the artifact packaging type from jar to war (see up)
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

META-INF / context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/hello-world-rest-demo"/>

BasicRestApplication.java

@SpringBootApplication
public class BasicRestApplication extends SpringBootServletInitializer {

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

    /*
    For starting the application in standalone TC
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BasicRestApplication.class);
    }

}

HelloWorldController.java

@RestController
@RequestMapping("/api")
public class HelloWorldController {

    //URI: http://localhost:8080/api/products
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public ResponseEntity<String> get() {
        return new ResponseEntity<>("Hello World", HttpStatus.OK);
    }

}

命令:

mvn spring-boot:run

运行应用程序并显示&#34; Hello World&#34;当我在浏览器中点击http://localhost:8080/api/hello

mvn clean install -DskipTests

在本地tomcat上手动部署战争并启动它。 浏览器中的http://localhost:8080/api/hello会提供404

2 个答案:

答案 0 :(得分:1)

path="/hello-world-rest-demo"中的{p> META-INF/content.xml可能会被忽略,请尝试使用网址中的上下文路径(在server.xml中指定)或war文件的名称,例如:

http://localhost:8080/<appName>/api/hello

答案 1 :(得分:0)

从context.xml中删除path属性,如果没有特定原因来维护它。而是完全删除context.xml文件。 Spring引导有助于独立于任何基于xml的配置。 请参阅this博客配置消失部分。 您应该可以从浏览器和放大器中看到您的REST API。嵌入