简体   繁体   中英

RestEasy returns 404 for every endpoint

I have created a web app Maven project that is using RestEasy as a RESTful application framework.

My web.xml file looks like this:

<?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.

But the problem is, my browser throws "404 not found", even though I have already specified the endpoint params as written in my classes.

Sample endpoint 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("/app")
  • @Path("app")

and the path inside the function test() to be:

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

But neither works.

The specification for my project:

  1. Java 1.7
  2. Tomcat 7.0
  3. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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