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

请求路径先报500后转404:未指定Servlet[jakarta.ws.rs.core.Application]类

问题分析与解决

问题现象

  • 访问任意路径时,首次请求返回500状态码,错误提示:No servlet class has been specified for servlet [jakarta.ws.rs.core.Application]
  • 再次访问同一路径时,返回404状态码

现有web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

<servlet>
        <!--        each project (in this case the bikeDealer) is bootstrapped with the a servlet-->
        <!--        this is basically the name of the servlet-->
        <servlet-name>jakarta.ws.rs.core.Application</servlet-name>

        <!--        firstly there should be servlet-class name to specify which class is being used by the servlet.-->
        <!--        Then there should be init param thingy that should specify which exact package to look for when-->
        <!--        the url has for example "bikes" after the "rest" thingy
                    ** BECAUSE then JERSEY WOULD KNOW WHICH PACKAGE TO LOOK FOR AND
                    THEN he can see which class has the annotation for the path-->

    </servlet>
    <servlet-mapping>
        <servlet-name>jakarta.ws.rs.core.Application</servlet-name>

        <!--        any request what is coming on this project (GET, POST, PUT, DELETE) is been handled by the this servlet class-->
        <!--        that servlet is basically mapped with the url pattern "rest", so after that "rest" what we put is handled by the
                    "JERSEY" servlet
                    What the JERSEY servlet do is basically it reads the code that we write (which is basically the class that we put after
                    the rest) and processes it. -->

        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>sign-in.html</welcome-file>
    </welcome-file-list>
</web-app>

问题原因

web.xml中仅定义了<servlet-name>,但未配置<servlet-class>,Servlet容器无法找到该名称对应的具体Servlet实现类。首次请求触发容器加载Servlet时,因找不到类抛出500错误;容器会标记该Servlet为不可用,后续请求直接返回404。

解决方案

由于这是Jersey(JAX-RS)应用,需要指定Jersey提供的Servlet容器类,并添加初始化参数指定资源类所在包。修改<servlet>节点内容如下:

<servlet>
    <servlet-name>jakarta.ws.rs.core.Application</servlet-name>
    <!-- 指定Jersey的Servlet实现类 -->
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <!-- 配置Jersey扫描的资源包路径 -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.example.bikedealer.rest</param-value>
    </init-param>
    <!-- 启动时加载Servlet,避免首次请求延迟 -->
    <load-on-startup>1</load-on-startup>
</servlet>

补充说明

  1. 确保pom.xml中已引入Jersey Servlet相关依赖(对应提供的pom配置)
  2. 将com.example.bikedealer.rest替换为你项目中存放带@Path注解的资源类的实际包路径
  3. <load-on-startup>标签设置为1,让容器在启动时就加载该Servlet,避免首次请求时的加载延迟

内容的提问来源于stack exchange,提问作者Martina Fernández

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 23:48:22