簡體   English   中英

HTTP tomcat maven 上的 WAR 文件部署后狀態 404,即使我的項目運行良好

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

當我“以 Spring 啟動應用程序運行”時,我的 spring-boot 項目運行良好,但是當我將它“Maven 安裝”到 WAR 文件時,然后將其部署到 tomcat 錯誤,如源服務器 - >目標資源的當前表示或不願意透露存在的表示。”。 即使我訪問了正確的 URL,正如我在使用“運行為 Spring 啟動應用程序”選項運行它時訪問的一樣(首先使用 WAR 文件名。當然:喜歡:“ Z80791B3AE7002CB88C24687/8xmlFget://localhost,776D9FAA/8xmlFget://localhost:)它仍然是404錯誤。

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

我的應用程序 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);
    }

}

和我的 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);
    }
}

我猜你的應用程序在你的 IDE 上運行時運行正常!

建議在您的pom.xml文件中添加<fileName>

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

現在,當您執行maven cleanmaven package時,目標目錄中將有一個帶有您提供的名稱的 war 文件。

您可以將其移動到 tomcat 安裝中的webapp目錄並啟動服務器。

現在,當您在應用程序中訪問 REST 調用時,您必須按如下方式調用它們!

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

您必須在 rest 調用中包含戰爭文件名 ( app-name )。

希望這可以幫助!

祝你好運!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM