You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jakarta EE环境下Vaadin Flow与其他Servlet共存的路径映射问题

在Payara Micro中配置Vaadin Flow映射到/ui/路径

因为application.properties是Spring Boot专属的配置文件,当你把应用打包成WAR部署到Payara Micro这类标准Jakarta EE容器时,这个文件会被直接忽略,所以得换成Jakarta EE标准的配置方式来指定Vaadin的URL映射,下面两种方法都能解决问题:

方法1:用web.xml配置

在项目的src/main/webapp/WEB-INF目录下创建或修改web.xml文件,添加Vaadin Servlet的映射规则:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">

    <!-- 配置Vaadin核心Servlet -->
    <servlet>
        <servlet-name>VaadinServlet</servlet-name>
        <servlet-class>com.vaadin.flow.server.VaadinServlet</servlet-class>
    </servlet>

    <!-- 把Vaadin请求映射到/ui/*路径 -->
    <servlet-mapping>
        <servlet-name>VaadinServlet</servlet-name>
        <url-pattern>/ui/*</url-pattern>
    </servlet-mapping>

    <!-- 这里配置你的其他Servlet,示例如下 -->
    <servlet>
        <servlet-name>MyCustomServlet</servlet-name>
        <servlet-class>com.example.yourpackage.MyCustomServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyCustomServlet</servlet-name>
        <url-pattern>/custom/*</url-pattern>
    </servlet-mapping>

</web-app>

这样配置后,Vaadin只会处理/ui/*开头的请求,你的其他Servlet可以通过各自的路径正常访问,不会被拦截。

方法2:用Servlet注解配置

如果你的项目没用到web.xml,可以直接用Java注解来配置:

  1. 创建Vaadin Servlet的子类,指定映射路径:
package com.example.yourpackage;

import com.vaadin.flow.server.VaadinServlet;
import jakarta.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = "/ui/*", name = "VaadinServlet", asyncSupported = true)
public class MyVaadinServlet extends VaadinServlet {
    // 不需要额外代码,继承即可
}
  1. 你的其他Servlet也用@WebServlet注解指定自己的路径,比如:
package com.example.yourpackage;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/custom/*")
public class MyCustomServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("来自自定义Servlet的响应");
    }
}

额外注意点

  • 你的Vaadin Flow 23.3.2和Payara Micro 5.2022.5版本是兼容的,两者都基于Jakarta EE规范,不用担心版本冲突
  • 部署后,Vaadin应用的访问路径是http://<你的主机>:<端口>/ui/,其他Servlet按照各自的映射路径访问即可

内容的提问来源于stack exchange,提问作者Sogawa-sps

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 19:50:12