简体   繁体   English

HTTP tomcat maven 上的 WAR 文件部署后状态 404,即使我的项目运行良好

[英]HTTP Status 404 after WAR file deployment on tomcat maven even my project is working fine

My spring-boot project is working fine when I "Run as Spring boot app", but when I "Maven install" it to WAR file, then deploy it to a tomcat server -> It always get 404 Error like "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.".当我“以 Spring 启动应用程序运行”时,我的 spring-boot 项目运行良好,但是当我将它“Maven 安装”到 WAR 文件时,然后将其部署到 tomcat 错误,如源服务器 - >目标资源的当前表示或不愿意透露存在的表示。”。 Even I access the correct URL as I accessed while run it with "Run as Spring boot app" option (With the WAR file name at first. Of course: Like: " http://localhost:7778/parsexml/getall "), It still 404 Error.即使我访问了正确的 URL,正如我在使用“运行为 Spring 启动应用程序”选项运行它时访问的一样(首先使用 WAR 文件名。当然:喜欢:“ Z80791B3AE7002CB88C24687/8xmlFget://localhost,776D9FAA/8xmlFget://localhost:)它仍然是404错误。

Here is my POM file这是我的 POM 文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.parsexml</groupId>
    <artifactId>parseXML</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>parseXML</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
    <finalName>parsexml</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

my App class我的应用程序 class

package com.parsexml.vnexpress;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class ParseXmlInsertdb1Application extends SpringBootServletInitializer{
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ParseXmlInsertdb1Application.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(ParseXmlInsertdb1Application.class, args);
    }

}

and my Controller和我的 Controller

package com.parsexml.vnexpress;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@RestController
public class getData {
    private final Logger logger = LoggerFactory.getLogger(getData.class);
    @Autowired
    private newRes res;

    @RequestMapping("getall")
    public List<db_url> getAlldata() {
        List<db_url> u = new ArrayList<>();
        res.findAll().forEach(u::add);
        return u;
    }

    @RequestMapping("/view/{id}")
    public Optional<db_url> getbyID(@PathVariable Long id){
        return res.findById(id);
    }

    @RequestMapping(method=RequestMethod.PUT, value="/update/{id}")
    public void update(@RequestBody db_url detail, @PathVariable Long id) {
        res.save(detail);
    }

    @RequestMapping(method=RequestMethod.DELETE, value="/delete/{id}")
    public void delete(@PathVariable Long id) {
        res.deleteById(id);
    }
}

I guess your app is running properly when you run it on your IDE!我猜你的应用程序在你的 IDE 上运行时运行正常!

It's recommended to add <fileName> in your pom.xml file.建议在您的pom.xml文件中添加<fileName>

<build>
    <finalName>app-name</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Now when you perform maven clean and maven package there will be a war file with the name you gave in the target directory.现在,当您执行maven cleanmaven package时,目标目录中将有一个带有您提供的名称的 war 文件。

You can move it to the webapp directory in your tomcat installation and start the server.您可以将其移动到 tomcat 安装中的webapp目录并启动服务器。

now when you are accessing the REST calls in your app, you have to call them as follows!现在,当您在应用程序中访问 REST 调用时,您必须按如下方式调用它们!

localhost:8088/app-name/first/get/call

you have to have the war file name ( app-name ) in your rest calls.您必须在 rest 调用中包含战争文件名 ( app-name )。

Hope this helps!希望这可以帮助!

Good luck!!祝你好运!!

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

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