【问题标题】:Jakarta http 404雅加达 http 404
【发布时间】:2023-02-14 20:40:27
【问题描述】:

我在这里最简单的代码中遇到问题:

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/test")
public class HelloResource {

    @GET
    public String sayHello(){
       return "hello";
    }
 }

应用:

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;

@ApplicationPath("/api")
public class MyApplication extends Application {

}

pom:

<dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-web-api</artifactId>
        <version>9.0.0</version>
        <scope>provided</scope>
    </dependency>
  
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.5.0</version>
    </dependency>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.5.2.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.1.2.Final</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

</dependencies>

我点击链接:http://localhost:8080/name_war/api/test

这给我:

Http 状态 404:请求的资源 [/javaEE_1_0_SNAPSHOT_war/api/test] 不可用

【问题讨论】:

    标签: java tomcat jax-rs


    【解决方案1】:

    答案很简单,我把它放在 web.xml 中,它起作用了。在那之前我没有接触 web.xml 因为我有 WebServlet 注释工作,这意味着 web.xml 版本>3.0,但事实证明不是,添加这个之后,它就可以了

     <servlet>
       <servlet-name>javax.ws.rs.core.Application</servlet-name>
     </servlet>
       <servlet-mapping>
         <servlet-name>javax.ws.rs.core.Application</servlet-name>
         <url-pattern>/api/*</url-pattern>
       </servlet-mapping>
    </web-app>
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 2019-09-08
      • 2021-11-09
      • 2021-03-06
      • 2022-07-22
      • 2020-08-05
      • 1970-01-01
      • 2013-10-03
      • 2021-06-09
      相关资源
      最近更新 更多