简体   繁体   中英

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. Getting 404 when I hit it through my browser. However, the same application when hit through embedded tomcat, works.

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);
    }

}

Command:

mvn spring-boot:run

Runs the application and display " Hello World " when I hit http://localhost:8080/api/hello in the browser

mvn clean install -DskipTests

Manually deploy the war on local tomcat and start it. http://localhost:8080/api/hello in the browser gives 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. Rather remove the context.xml file altogether. Spring boot facilitates independence from any xml based configuration. Refer this blog's Config goes away section. You should see your REST api accessible from both the browser & embedded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM