Search code examples
javajakarta-eenetbeansnetbeans-8java-ee-7

cannot find route javaEE


I have created new web- project in netbeans ( new project - java web - web application || name : test || server: glassFish server 4.1.1 || JavaEE version: JavaEE 7 web || ContextPath: /Test)

It generates html file where i add simple ajax call to server.

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
        <script>
            fetch('/Test/name').then(function(x){
                console.log(x)
            })
        </script>
    </body>
</html>

Now ( without script tag ) when i run the project it thrwos me on

localhost:8080/Test

Now i create a new java class

@Path("/Test")
@Produces("text/plain")
public class RequestClass {
     private String name = "MyName";
     private String age = "MyAge";
    @GET
    @Path("/name")
    @Produces("text/plain")
    public String getName(){
        return this.name;
    }
    @GET
    @Path("/age")
    @Produces("text/plain")
    public String getAge(){
        return this.age;
    }
}

Now when i build and run project , the server respond with 404

http://localhost:8080/Test/name Failed to load resource: the server responded with a status of 404 (Not Found)

Why is this happening? The route is correct , so why it cannot find it?

I am trying to fix it for a while now but cannot find anything about it.

I tried to use just

    fetch('/name').then(function(x){
        console.log(x)
    })

but didnt work too.

Thanks for help!


Solution

  • What is your application name or application context root? you need to have your application context root in the url to work, E.g domain:port//routes, for your case it will be http://localhost:8080/application-context-root/Test/name

    Jersey configuration

    <servlet>
        <servlet-name>jersey-serlvet</servlet-name>
        <servlet-class>
                     com.sun.jersey.spi.container.servlet.ServletContainer
                </servlet-class>
        <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>com.mkyong.rest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>