首页 > 解决方案 > 在 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>


   

标签: jax-rshttp-status-code-404

解决方案


推荐阅读