[英]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.