【问题标题】:Why error 404 even though resource exists (jersey)?即使资源存在(球衣),为什么会出现错误 404?
【发布时间】:2022-01-05 00:37:24
【问题描述】:

我想将我的 Raspberry Pi 设置为 Web 服务器。使用 ngrok 和 node.js 我已经设置了服务器部分,我可以访问我在那里编写的网页(前端)。

但是,后端部分给我带来了很多麻烦。我对此很陌生,根据我经历的一些教程,我将球衣用于后端。我创建了一个简单的测试来查看我的实现是否正常工作 - 通过 POST 请求发送用户名并检查长度是否大于 0。

我的 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>

我的 MyResource.java 文件 -

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";
    }
}

我的 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>

当我输入用户名并点击验证时,我收到 404 'resource not found' 错误。

我做错了什么?请帮助我了解我的实施中出了什么问题。

【问题讨论】:

    标签: javascript java html rest post


    【解决方案1】:

    在试图找出我的问题令人沮丧的一周之后,我终于明白出了什么问题。

    在我的 pom 文件中存在以下内容 -

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

    所以对于每一个新资源,我都需要访问 localhost:8080/resttest/webapi* 我省略了“/resttest”,只关注 web.xml 文件的 URL 模式。

    不幸的是,我在发布问题后发现了这一点。由于我在这里看到了很多类似的问题,所以我将在此回复。我希望它对将来的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2022-01-25
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2020-02-26
      • 2018-07-08
      相关资源
      最近更新 更多