为什么即使资源存在(球衣)也会出现错误 404?
Why error 404 even though resource exists (jersey)?
我想将我的 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' 错误。
我哪里做错了?请帮助我理解我的实现有什么问题。
经过令人沮丧的一周尝试确定我的问题后,我终于明白问题出在哪里了。
在我的 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 模式。
不幸的是,我在发布问题后发现了这一点。由于我在这里看到了很多类似的问题,所以我会把这个和这个回复留在这里。我希望它能对以后的人有所帮助。
我想将我的 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' 错误。
我哪里做错了?请帮助我理解我的实现有什么问题。
经过令人沮丧的一周尝试确定我的问题后,我终于明白问题出在哪里了。
在我的 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 模式。
不幸的是,我在发布问题后发现了这一点。由于我在这里看到了很多类似的问题,所以我会把这个和这个回复留在这里。我希望它能对以后的人有所帮助。