SonOfEl
SonOfEl

Reputation: 177

Why error 404 even though resource exists (jersey)?

I want to setup my Raspberry Pi as a web server. Using ngrok and node.js I've setup the server part and I can access webpages I've written there (frontend).

However, the backend part is causing me a lot of trouble. I am very new to this and based on some tutorials I went through I'm using jersey for the backend. I've created a simple test to see if my implementation is working - send a username via POST request and check if length is greater than 0.

My JSP-

<html>
<head>
<script type=text/javascript>
    function verify()
    {
        var xhr = new XMLHttpRequest();
        var u = document.getElementById("uname").value;
        //alert(u);
        xhr.open('POST', 'http://localhost:8080/webapi/myresource');
        xhr.setRequestHeader('Content-Type', 'text/plain');
        xhr.setRequestHeader("Accept", "text/plain");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                alert(xhr.response);
            }
        }
        xhr.send(u);
    }

</script>
</head>

<body>
    <h3>User Login/Register</h3>
    <label>Username: </label>
    <input type="text" placeholder="Enter username" name="uname" id="uname" required>
    <button onclick="verify()" id="v" >Check/Verify</button>
    <br><label>Password: </label>
    <input type="password" placeholder="Enter password" name="pwd" required>
    <br><button type="submit">Login/Register</button>
</body>
</html>

My MyResource.java file -

package com.X.X;

@Path("myresource")
public class MyResource {
    
    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public String verify(String uname)
    {
        if (uname.length()>0)
            return "Verified";
        
        return "Unverified";
    }
}

My web.xml -

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.resttest.resttest</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>

When I type in the username and click on verify, I get the 404 'resource not found' error.

webpage+error

What am I doing wrong? Please help me understand what is wrong in my implementation.

Upvotes: 1

Views: 427

Answers (1)

SonOfEl
SonOfEl

Reputation: 177

After a frustrating week trying to identify my problem, I finally understood what's wrong.

In my pom file the following exists -

    <groupId>com.resttest</groupId>
    <artifactId>resttest</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>resttest</name>

So for every new resource, I need to go to localhost:8080/resttest/webapi* I was omitting "/resttest" and focusing only on web.xml file's URL pattern.

Unfortunately I identified this after posting my question. Since I saw quite a few questions similar to this here, I'll leave this here with this reply. I hope it will help somebody in the future.

Upvotes: 1

Related Questions