如何解决部署到 Tomcat 10 后无法访问 Spring Boot 应用程序
我一直在尝试转换现有的 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;
}
}
解决方法
可能是因为您错误地传递了上下文路径 kantha
两次?
http://localhost:8585/kantha/kantha/api/v1/items
尝试通过以下方式访问。
http://localhost:8585/kantha/api/v1/items
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。