简体   繁体   English

RestEasy为每个端点返回404

[英]RestEasy returns 404 for every endpoint

I have created a web app Maven project that is using RestEasy as a RESTful application framework. 我创建了一个Web应用程序Maven项目,该项目使用RestEasy作为RESTful应用程序框架。

My web.xml file looks like this: 我的web.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>me.randytan.inconium</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/v1/api</param-value>
  </context-param>
  <context-param>
      <param-name>resteasy.scan</param-name>
      <param-value>true</param-value>
   </context-param>
  <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
    </listener-class>
  </listener>
  <servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/v1/api/*</url-pattern>
  </servlet-mapping>
</web-app>

I have set the automatic scan to true and created a few classes that cater for my endpoint access. 我将自动扫描设置为true,并创建了一些可以满足我的端点访问需求的类。

But the problem is, my browser throws "404 not found", even though I have already specified the endpoint params as written in my classes. 但是问题是,即使我已经指定了类中编写的端点参数,我的浏览器也会抛出“ 404 not found”。

Sample endpoint URL: http://localhost:8080/me.randytan.inconium/v1/api/app/test 样本端点URL: http:// localhost:8080 / me.randytan.inconium / v1 / api / app / test

This is my sample class i made: 这是我制作的示例课:

package me.randytan.inconium.controller;

import java.sql.Connection;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("app")
public class AppTransaction {

    public AppTransaction(){ //do something
    }

    @GET
    @Path("/test")
    public String test(){
        return "Hello World";
    }
    ...

I have tried to change the @Path in the class to be: 我试图将类中的@Path更改为:

  • @Path("/app")
  • @Path("app")

and the path inside the function test() to be: 函数test()内部的路径为:

  • @Path("/test")
  • @Path("test")

But neither works. 但是两者都不起作用。

The specification for my project: 我的项目的规格:

  1. Java 1.7 Java 1.7
  2. Tomcat 7.0 Tomcat 7.0
  3. RestEasy 3.0.14 RestEasy 3.0.14

I finally managed this. 我终于做到了。

It is better to declare the listener class one by one by creating an Main Application and called the main application on the web.xml 最好通过创建一个Main Application并在web.xml上调用main应用程序来声明一个侦听器类。

Sample: 样品:

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;


public class MainApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();
        private Set<Class<?>> empty = new HashSet<Class<?>>();

        public MainApplication() {
            //generate the main class of the framework;
            try{
                //custom function. not necessary
            } catch (Exception e){
                e.toString();
            }

            //add singletons to Restful Transaction API;
            this.singletons.add(new AppTransaction());
        }

        public Set<Class<?>> getClasses()
        {
            return this.empty;
        }

        public Set<Object> getSingletons()
        {
            return this.singletons;
        }
}

then call the class Application inside the web.xml 然后在web.xml调用类Application

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
            org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
    <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>me.randytan.inconium.controller.MainApplication</param-value>
    </init-param>
  </servlet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM