繁体   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