jax-rs - 在 JAX-RS Web 服务/Maven/Eclipse/Tomcat 9 上出现 404 错误
问题描述
我正在尝试使用 AlienResouces 作为 Resource 以 XML 格式显示 Alien 对象。但是,它在浏览器运行时给了我 404 错误,并且调用的调试 stmnt GeAlien没有显示在 Tomcat 控制台上
网络服务
package com.dip.testproj;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Alien {
private String name;
private int points;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
以下是 Alien 对象的资源。Alien 对象正在获取资源名称和编号,并试图在运行时将其显示在浏览器上。
我们服务资源
package com.dip.testproj
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("aliens")
public class AlienResources {
@GET
@Produces(MediaType.APPLICATION_XML)
public Alien getAlien(){
System.out.println("getAlien Called");
Alien a1 = new Alien();
a1.setName("Navin");
a1.setPoints(60);
return a1;
}
}
Tomcat 的控制台上没有编译错误或任何错误。服务器。但是,尝试在 FF 浏览器上运行THIS时,它会给出“404”或“找不到资源”
WEB.xml 上的 URL 模式如下所示。
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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.dip.testproj</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>
</web-app>
解决方案
推荐阅读
- python - psutil.AccessDenied:psutil.AccessDenied (pid=7868)
- reactjs - 制表符在行单击时执行不需要的滚动
- javascript - 路径交叉导致部分消失【D3路径生成】
- javascript - 如何返回正确的 JavaScript 提取错误
- python - 如何在 PySpark 中按行将 spark df(大 df)拆分为多个 dfs?
- ios - Swift Avplayer - 当点击“播放”时,停止所有其他 tableviewcells 播放
- javascript - 将 React 组件动态添加到 DIV 或其他 React App 中
- gcc - Meteor 应用程序无法找到 Fibers 二进制文件;然而 NPM 构建了一个不同的
- regex - 正则表达式在 Excel 中查找工作表引用
- c# - 如何在 Unity 2d 中保持恒定速度?