使用 jakarta.* 包在 Tomcat 10.x 上部署 Spring 5.x [英] Deploying Spring 5.x on Tomcat 10.x with jakarta.* package

查看:95
本文介绍了使用 jakarta.* 包在 Tomcat 10.x 上部署 Spring 5.x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR:我有一个适用于 Tomcat 9 的 Spring MVC helloworld 应用程序.Tomcat 10 上的同一个应用程序为 Web 请求映射提供了 404 错误.

TL;DR: I have Spring MVC helloworld app that works on Tomcat 9. The same app on Tomcat 10 gives a 404 error for web request mappings.

问题

将 Spring MVC 5 helloworld 应用程序部署到 Tomcat 10 时,该应用程序为 Web 请求映射提供 404 错误.相同的 helloworld 应用程序适用于 Tomcat 9.它在 Tomcat 9 上显示 helloworld 消息.

When deploying a Spring MVC 5 helloworld app to Tomcat 10, the app gives a 404 error for web request mappings. The same helloworld app works on Tomcat 9. It displays the helloworld message on Tomcat 9.

我所期望的

我希望应用程序在 Tomcat 10 上显示 helloworld 消息.

I expected the app to display the helloworld message on Tomcat 10.

环境

  • 微软视窗 10
  • Tomcat 10.0.2
  • Spring MVC 5.3.3

我进行的研究

我在 Spring 参考手册的 Web Servlet 部分进行了研究.我还在线测试了 Spring MVC 教程.这些教程适用于 Tomcat 9.但是,相同的教程在 Tomcat 10 上失败了.我还在 Tomcat 10 上执行了谷歌搜索.我看到了对 Jakarta EE 的引用,但不确定这是否是问题的根源.Java EE 8 和 Jakarta EE 8 向后兼容.

I researched in the Spring Reference Manual, Section on Web Servlet. I also tested Spring MVC Tutorials onlines. These tutorials worked on Tomcat 9. However, the same tutorials failed on Tomcat 10. I also performed google search on Tomcat 10. I saw references to Jakarta EE but not sure if this is the source of the issue. Java EE 8 and Jakarta EE 8 are backwards compatible.

如何复制

我创建了一个非常基本的 helloword 项目来测试这一点.这是我用于项目的代码.

I created a very basic helloword project to test this out. Here is the code that I'm using for the project.

pom.xml

<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.example.spring</groupId>
    <artifactId>example-spring</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.3</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>
        </plugins>
    </build>
</project>

ProjectInitializer.java

ProjectInitializer.java

package com.example;

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

public class ProjectInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
    
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }
}

PureJavaConfig.java

PureJavaConfig.java

package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("com.example")
public class PureJavaConfig {

    @Bean
    public ViewResolver viewResolver() {    
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".jsp");
        resolver.setPrefix("/WEB-INF/jsp/");
        return resolver;
    }
    
}

教程控制器.java

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TutorialController {

    @GetMapping("/")
    public String home() {
        return "home";
    }
}

home.jsp

<html>
<body>Hello World of Spring! <%= java.time.LocalDateTime.now() %></body>
</html>

这个项目在 Tomcat 9 上运行正常.它显示 helloworld 消息.但是,当我在 Tomcat 10 上运行时,收到 404 错误消息.

This projects runs okay on Tomcat 9. It displays the helloworld message. However, when I run on Tomcat 10, I get a 404 error message.

推荐答案

TL;DR:Spring MVC 5 不能在 Tomcat 10 上运行,因为包从 javax.* 重命名为jakarta.*.

TL;DR: Spring MVC 5 does not run on Tomcat 10 because of the package renaming from javax.* to jakarta.*.

经过进一步研究,我找到了问题的答案.Spring MVC 5 不能在 Tomcat 10 上运行.这是因为 Tomcat 10 基于 Jakarta EE 9,其中 API 的包名称已从 javax.* 更改为 雅加达.*.

After further research, I was able to find the answer to my question. Spring MVC 5 does not work on Tomcat 10. This is because Tomcat 10 is based on Jakarta EE 9 where package names for APIs have changed from javax.* to jakarta.*.

Tomcat 10 在下载网页上提到了这一点:

Tomcat 10 mentioned this on the download webpage:

Tomcat 10 以后的用户应该知道,由于从 Java EE 迁移到 Jakarta EE 作为 Java EE 迁移到的一部分Eclipse Foundation,所有已实现 API 的主要包已从 javax.* 更改为 jakarta.*.这几乎肯定会需要更改代码才能使应用程序从 Tomcat 9 迁移以及更早的 Tomcat 10 及更高版本.

Users of Tomcat 10 onwards should be aware that, as a result of the move from Java EE to Jakarta EE as part of the transfer of Java EE to the Eclipse Foundation, the primary package for all implemented APIs has changed from javax.* to jakarta.*. This will almost certainly require code changes to enable applications to migrate from Tomcat 9 and earlier to Tomcat 10 and later.

对于 Spring MVC 5,Spring MVC DispatcherServlet 依赖于 javax.servlet.* 包命名空间.这是使用 Java EE 8 javax 包命名.由于 Tomcat 10 基于 Jakarta EE 9,不支持用于 javax 命名的包.这就解释了为什么 Spring MVC 5 在 Tomcat 10 上不起作用.

For Spring MVC 5, the Spring MVC DispatcherServlet has a dependency on the javax.servlet.* package namespace. This is using the Java EE 8 javax package naming. Since Tomcat 10 is based on Jakarta EE 9, the packages for javax naming are not supported. This explains why Spring MVC 5 does not work on Tomcat 10.

针对 Spring 框架提交了与此相关的 github 问题:

There are github issues filed against the Spring Framework regarding this:

Spring 核心 5 未在 tomcat 10 上启动

支持 Jakarta EE 9(jakarta 中的注释和接口.*命名空间)

就我而言,我不会迁移到 Tomcat 10,而是继续使用 Tomcat 9,直到 Spring 框架升级到 Jakarta EE 9.

In my case, instead of migrating to Tomcat 10, I will stay on Tomcat 9 until the Spring framework is upgraded to Jakarta EE 9.

这篇关于使用 jakarta.* 包在 Tomcat 10.x 上部署 Spring 5.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆