2

我一直在尝试转换现有的 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;
    }
}

4

1 回答 1

0

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

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

尝试通过以下方式访问。

http://localhost:8585/kantha/api/v1/items
于 2021-02-21T20:34:53.797 回答