部署到 Tomcat 10 后无法访问 Spring Boot 应用程序 [重复]

Posted

技术标签:

【中文标题】部署到 Tomcat 10 后无法访问 Spring Boot 应用程序 [重复]【英文标题】:Cannot Access Spring Boot App After Deploying to Tomcat 10 [duplicate] 【发布时间】:2021-05-24 03:41:00 【问题描述】:

我一直在尝试转换现有的 Springboot 应用程序以使其可部署到 Tomcat 10。它可以很好地部署到 Tocat 但是当我尝试像这样访问这里 http://localhost:8585/kantha/kantha/api/ v1/items 我得到 404 not found 错误。

如果我将它作为 Spring Boot 应用程序运行,它会按预期工作 http://localhost:8787/kantha/api/v1/items

我在开发者工具和 catalina 日志中都没有发现任何错误。

这是我的申请文件


package com.entrustment.entrustment.sites; 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@SuppressWarnings("unchecked", "deprecated")
@SpringBootApplication
@EnableJpaAuditing
@EntityScan("com.entrustment.entrustment.sites.domain")
//@ComponentScan(basePackages= "mik.miksubishi.domain")
public class EntrustmentApplication extends SpringBootServletInitializer 
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) 
        return application.sources(EntrustmentApplication.class);
    

    public static void main(String[] args) 
        SpringApplication.run(EntrustmentApplication.class, args);  
    

   @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() 
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.featuresToEnable(SerializationFeature.WRAP_ROOT_VALUE); // enables wrapping for root elements
        return builder;
    
 
    @Bean
    public WebMvcConfigurer corsConfigurer() 
        return new WebMvcConfigurer() 
            @Override
            public void addCorsMappings(CorsRegistry registry) 
                registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("*");
            
        ;
    


这是我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.companyname</groupId>
    <artifactId>kantha</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>kantha</name>
    <description>Kantha</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath />
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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-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-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>   
        
        <dependency> 
          <groupId>com.google.guava</groupId> 
          <artifactId>guava</artifactId> 
          <version>22.0</version> 
        </dependency>           
    </dependencies>

    <build>
        <finalName>$artifactId</finalName>
        <plugins>
                <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>           
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<configuration>
<mainClass>org.apache.openjpa.jdbc.meta.ReverseMappingTool</mainClass>
<commandlineArgs>
    -directory src/main/java -accessType fields
    -useGenericCollections true -package org.yourproject.model
    -metadata none -annotations true
    -innerIdentityClasses false -useBuiltinIdentityClass false
    -primaryKeyOnJoin false
    </commandlineArgs>
<includePluginDependencies>true</includePluginDependencies>
</configuration>
<dependencies>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.CR3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa-all</artifactId>
        <version>2.0.1</version>
    </dependency>
</dependencies>
    </plugin>
            
        </plugins>
    </build>
</project>

我的网络初始化器

package com.entrustment.entrustment.sites.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class EntrustmentAppWebAppInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer 


      @Override
      protected String[] getServletMappings() 
        return new String[]  "/" ;  // Standard: Map dispatcher to URLs with /content
      

      @Override
        protected Class<?>[] getServletConfigClasses() 
      return new Class<?>[]  WebConfig.class ;
      

    @Override
    protected Class<?>[] getRootConfigClasses() 
        // TODO Auto-generated method stub
        return null;
    


【问题讨论】:

我的 applications.properties 文件有这个 server.servlet.context-path=/kantha/api/v1 删除你的EntrustmentAppWebAppInitializer。那和您的应用程序可用添加http://localhost:8585/&lt;name-of-war&gt;/kantha/api/v1/items 。因此,除非您的战争被命名为 kantha.war,否则 URL 不正确。 我对我的重复建议很认真。这可能不是很明显,但如果我得到正确的版本号,您需要 Spring Boot 3 才能与 jakarta EE 兼容。当您使用 Tomcat 10 时,这就是您所需要的。但是你使用的是 Spring Boot 2。 【参考方案1】:

因为可能是您错误地传递了上下文路径kantha 两次?

http://localhost:8585/kantha/kantha/api/v1/items

尝试通过以下方式访问。

http://localhost:8585/kantha/api/v1/items

【讨论】:

我应该提到我尝试了localhost:8585/kantha/api/v1/items 和localhost:8585/kantha/kantha/api/v1/items 都没有运气。

以上是关于部署到 Tomcat 10 后无法访问 Spring Boot 应用程序 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

将项目部署到tomcat的ROOT目录下无法访问(tomcat9.0.21)

CentOS6.10部署的Tomcat8.5启动后,浏览器访问不到的解决方法

spring boot 部署war 到tomcat上面静态资源无法访问

项目部署到tomcat上,tomcat能正常启动,为啥却无法访问?

javaweb项目部署到linux的tomcat无法访问到,提示404.

Elastic Beanstalk 上的 Tomcat 部署:无法访问该站点