简体   繁体   English

在独立的tomcat上使用Spring Boot开发的REST应用程序命中时获取404

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

I am not able to hit my REST api developed using Spring Boot and Java 8 on a standalone tomcat. 我无法在独立的tomcat上运行使用Spring Boot和Java 8开发的REST api。 Getting 404 when I hit it through my browser. 通过浏览器访问404时得到它。 However, the same application when hit through embedded tomcat, works. 但是,当通过嵌入式tomcat进行访问时,相同的应用程序也可以工作。

pom.xml 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 META-INF / context.xml

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

BasicRestApplication.java 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 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);
    }

}

Command: 命令:

mvn spring-boot:run

Runs the application and display " Hello World " when I hit http://localhost:8080/api/hello in the browser 当我在浏览器中点击http://localhost:8080/api/hello ,运行应用程序并显示“ Hello World

mvn clean install -DskipTests

Manually deploy the war on local tomcat and start it. 在本地tomcat上手动部署战争并启动它。 http://localhost:8080/api/hello in the browser gives 404 浏览器中的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

Remove the path property from context.xml, if not specific reason to maintain it. 如果不是特殊原因,请从context.xml中删除path属性。 Rather remove the context.xml file altogether. 而是完全删除context.xml文件。 Spring boot facilitates independence from any xml based configuration. Spring Boot促进了与任何基于xml的配置的独立性。 Refer this blog's Config goes away section. 请参阅博客的“ 配置消失”部分。 You should see your REST api accessible from both the browser & embedded. 您应该看到可以从浏览器和嵌入式设备访问的REST api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用Spring开发的Rest API时获取404 - Getting 404 while calling rest api developed in Spring 使用 Spring Boot + REST 应用程序出现 404 错误 - Getting 404 error with Spring Boot + REST application 在Tomcat 8上获取运行404的本地Spring Boot应用程序 - Getting a 404 running local spring boot application on Tomcat 8 在 Spring 启动 2.2.0 应用程序中遇到 REST 端点的问题 - Problem hitting REST endpoint in Spring Boot 2.2.0 application 无法在独立Tomcat 8.5上启动Spring Boot 1.5.1应用程序 - Unable to start Spring Boot 1.5.1 application on Standalone Tomcat 8.5 Spring Boot应用程序可以独立运行,但是在Tomcat中部署时找不到类 - Spring Boot application works as standalone but class not found when deployed in Tomcat Spring Boot应用程序在Intellij上启动,但在Tomcat独立服务器上无法启动[Gradle] - Spring Boot Application starts on Intellij but not on Tomcat standalone [Gradle] Spring 启动应用程序战争不工作,在 tomcat 服务器上部署时获取 404 - Spring boot application war not working, Getting 404 when deplying on tomcat server 尝试部署简单的 hello spring 启动应用程序时出现 Tomcat 404 错误 - Getting Tomcat 404 error while trying to deploy simple hello spring boot app Spring Boot应用程序通常在本地的tomcat 8上正常启动,但我得到404 - Spring boot application is starting normally on tomcat 8 locally but i am getting 404
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM