簡體   English   中英

在獨立的tomcat上使用Spring Boot開發的REST應用程序命中時獲取404

[英]Getting 404 while hitting my REST application developed using Spring Boot on standalone tomcat

我無法在獨立的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

當我在瀏覽器中點擊http://localhost:8080/api/hello ,運行應用程序並顯示“ Hello World

mvn clean install -DskipTests

在本地tomcat上手動部署戰爭並啟動它。 瀏覽器中的http://localhost:8080/api/hello提供404

可能會忽略META-INF/content.xml path="/hello-world-rest-demo" ,請嘗試使用URL或war文件名稱中的上下文路徑(在server.xml中指定),例如:

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

如果不是特殊原因,請從context.xml中刪除path屬性。 而是完全刪除context.xml文件。 Spring Boot促進了與任何基於xml的配置的獨立性。 請參閱博客的“ 配置消失”部分。 您應該看到可以從瀏覽器和嵌入式設備訪問的REST api。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM