繁体   English   中英

Rest API 响应 404 Not found when run project with Java Application

[英]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. 我不知道为什么

这是我的主要应用程序

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

这是我的文件 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>

这是我的 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

{
    "timestamp": "2020-05-28T08:03:05.090+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/MySecret/demo/user"
}

但是当我通过Tomcat Server运行我的应用程序时,它仍然成功

{
    "code": "R_200",
    "message": "Your request has been processed successfully.",
    "totalRecord": 0,
    "data": "Ok"
}

我不想再使用Tomcat Server来运行我的应用程序了,请有人帮帮我!!!

当您以 jar(java 应用程序)运行项目时,您应该使用 localhost:8080/demo/user

当您将应用程序运行到服务器(例如 jboss)时,war 文件的名称是根路径。

本地主机:8080/MySecret/demo/user

我认为您的战争文件是 MySecret.war。

这就是第一个和第二个不同的原因。 由于您没有基本路径,因此 /demo/user 是您的端点的路径。

暂无
暂无

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

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