[英]Rest API response 404 Not found when run project with Java Application
I have app with spring boot, i want to run this app with Java Application
but when i call rest API it always response 404, but with Tomcat server
it's ok. I have app with spring boot, i want to run this app with
Java Application
but when i call rest API it always response 404, but with Tomcat server
it's ok. I don't know why我不知道为什么
Here is my main app这是我的主要应用程序
package com.secret;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class SecretApplication extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(SecretApplication.class, args);
}
}
here is my file pom.xml这是我的文件 pom.xml
<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.secret</groupId>
<artifactId>MySecret</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here is my controller这是我的 controller
package com.secret.demo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.secret.common.dto.ResponseBodyDto;
@CrossOrigin
@RestController
@RequestMapping("/demo")
public class Demo {
@GetMapping(value = "/user")
public ResponseEntity<Object> getUserByUsername() throws Exception{
ResponseBodyDto<Object> dto = new ResponseBodyDto<>();
dto.setData("Ok");
return new ResponseEntity<Object>(dto, HttpStatus.OK);
}
}
And When i run my app by Java Application
always get 404 when call rest API http://localhost:8080/MySecret/demo/user And When i run my app by
Java Application
always get 404 when call rest API http://localhost:8080/MySecret/demo/user
{
"timestamp": "2020-05-28T08:03:05.090+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/MySecret/demo/user"
}
But when i run my app by Tomcat Server
then it still success但是当我通过
Tomcat Server
运行我的应用程序时,它仍然成功
{
"code": "R_200",
"message": "Your request has been processed successfully.",
"totalRecord": 0,
"data": "Ok"
}
I don't want to use Tomcat Server
to run my app anymore, Someone help me pls!!!我不想再使用
Tomcat Server
来运行我的应用程序了,请有人帮帮我!!!
When you run your project as jar (java application), you should use localhost:8080/demo/user当您以 jar(java 应用程序)运行项目时,您应该使用 localhost:8080/demo/user
When you run your application into a server (jboss per example) the name of your war file is the root path.当您将应用程序运行到服务器(例如 jboss)时,war 文件的名称是根路径。
localhost:8080/MySecret/demo/user本地主机:8080/MySecret/demo/user
I think your war file is MySecret.war.我认为您的战争文件是 MySecret.war。
That's the reason of the difference between the first and the second.这就是第一个和第二个不同的原因。 Since you don't have a base path, /demo/user is the path of your endpoint.
由于您没有基本路径,因此 /demo/user 是您的端点的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.