Home > database >  Jakarta http 404
Jakarta http 404

Time:09-03

I have a problem in the simplest code here:

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

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

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

Application:

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>

I click on the link: http://localhost:8080/name_war/api/test

And this give to me:

Http staus 404 : The requested resource [/javaEE_1_0_SNAPSHOT_war/api/test] is not available

CodePudding user response:

The answer was simple enough, I put it in web.xml and it worked. Before that I didn't touch web.xml because I had WebServlet annotation working, which meant web.xml version >3.0, but it turns out not, after adding this, it all worked

 <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>
  • Related