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

问题描述 投票:0回答:1

当我“以Spring启动应用程序运行”时,我的spring-boot项目运行正常,但是当我“将其安装”到WAR文件中,然后将其部署到tomcat服务器时,->总是会出现404错误,例如“原始服务器找不到目标资源的当前表示或不愿意透露其存在。”。甚至我访问的URL都与使用“以Spring方式启动应用程序运行”选项运行时访问的URL一样(首先带有项目名称!当然)。

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

我的应用程序类别

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

}

和我的控制器

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);
    }
}
java maven spring-boot war tomcat9
1个回答
0
投票

我猜您的应用程序在IDE上运行时运行正常!

建议在<fileName>文件中添加pom.xml

<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

您在其余​​呼叫中必须具有war文件名(app-name)。

希望这会有所帮助!

祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.