0
votes

I have lost the ability to develop/test my RESTful JAX-RS web services when running in Eclipse, using Tomcat 9.0.31. When I start up tomcat inside eclipse and try and hit any one of a number of JAX-RS wed services I have written, I receive a 404 error. For this type of testing I generally use Fiddler to hit the service URL

I have read through a number of posts here on SO including, but not limited to:

and more. Most of the responses appear to be for servlet 2.5 and earlier and refer to using the servlet description in the web.xml.

My more recent services are all annotated, and I do not need to add anything to the web.xml.

My web.xml starts out with

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">

The things I have tried are numerous. For instance, I was using OpenJDK 13 as my java SDK, so I changed my default JRE in eclipse to my OpenJDK 8 (1.8) which did not help. I've done a number of project clean/builds, and maven project updates.

One thing I read and already knew of was setting the metadata-complete attribute to false in the web.xml header. That was already set, so I removed it. Still no good. I tried upgrading my Jersey Bundle dependency version in my pom, no good.

I did try overriding the getClasses() method

    @Override
    public Set< Class< ? > > getClasses() {

        final Set< Class< ? > > returnValue = new HashSet< Class< ? > >();
        returnValue.add( TestApi.class );
        return returnValue;
    }

which also did not work. Note that after each change I stopped and started my Tomcat server inside Eclipse.

So here is the pertinent information:

Eclipse project facets:

  • Dynamic Web Module 4.0
  • Java 1.8
  • JavaScript 1.0

I've already posted the web.xml header above

pom.xml:

<dependency>
    <groupId>org.glassfish.jersey.bundles</groupId>
    <artifactId>jaxrs-ri</artifactId>
    <version>2.32</version> <-- Note I was using version 2.29 this is the version I upgraded too.
</dependency>

Application Subclass:

package com.company;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath ( "resources" )
public class ApplicationConfig extends Application {}

Test Class:

package com.company;

import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path ( "test" )
public class TestApi {

    @GET
    @Path ( "datetime" )
    public String getDate() {

        return new Date().toString();
    }
}

When I run my web app I expect to be able to receive the current server date/time by calling http://localhost:8080/resources/test/datetime. But I get a 404 error and the message is simply "The requested resource was not found on this server. This error is most commonly caused by a mistyped URL or a broken link". No error in the Eclipse console.

Typically, when Tomcat starts I tend to see each service listed, as it is discovered, on the start output after the 8080 protocol start message:

enter image description here

I am stuck and I need to check on a recent change to another class which is not working on the server.

What can I do to restore this functionality?

1
HTTP 404 means the URL you asked for is not available on the server. None of the info you've provided explains it. I would guess that you have a packaging issue OR the URL you entered in the browser is not correct. Are you using the Maven JAR plug in to create your executable JAR? Did you open the JAR to see if the contents were correct?duffymo
@duffymo, this is all within Eclipse. While we do use maven to package a WAR file, for debugging and development, I simply run the web application in EclipsePaul Stoner
In addition, I have copied each part of the URL and pasted into Fiddler to test the endpoint, so an incorrect URL seems unlikelyPaul Stoner

1 Answers

0
votes

The issue turned out to be the JAX-RS Project Facet was not selected for the project.

For those running into the same, or similar, issue right click in the project explorer, assuming using eclipse, and select "Properties". Then select "Project Facets" from the left hand panel of properties. Finally, check "JAX-RS (REST Web Service)" and set the version dropdown to the appropriate implementation version. For my project I use version 2.1