I'm creating a REST service in Java. I use javax package. I have the following question: is it possible to create something like "default page" for the unimplemented endpoints? I.e., let us suppose I implement the following endpoints:
localhost:8080/rest/a
localhost:8080/rest/b
And know I want some default page to be loaded if a user enters e.g. localhost:8080/rest/c (maybe some redirect for 404 error?). I looked for something like this in Google but with no success. I have no idea what I could start from. Thank you for any help!
EDIT:
I would like to mention that I tried to add to my web.xml something like this:
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/404.xhtml</location>
</error-page>
but unfortunately I doesn't work for REST addresses (localhost:8080/rest/something). However, it does work for any other addresses.
EDIT2:
I put some code snippets:
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("rest")
public class JaxRsApplication extends Application {
}
===========================================================
@Path("/myview")
public class MyView {
@GET
@Path("/")
public Response root() throws NotImplementedException {
throw new NotImplementedException();
}
@POST
@Path("/test")
public Response test() throws NotImplementedException {
throw new NotImplementedException();
}
}
EDIT3:
@cássio-mazzochi-molin sorry for a delay, I couldn't check this. But now I have checked this and I'm not sure if this is what I want.
I've implemented EntityNotFoundExceptionMapper:
@Provider
public class EntityNotFoundExceptionMapper implements ExceptionMapper<EntityNotFoundException> {
@Override
public Response toResponse(EntityNotFoundException exception) {
return Response.status(404).entity(exception.getMessage()).type("text/plain").build();
}
}
and added it to the Application:
@ApplicationPath("rest")
public class JaxRsApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(EntityNotFoundExceptionMapper.class));
}
}
But there is a problem - if I want it to work, I have to define an endpoint, let's say "C" and throw there EntityNotFoundException. But I want it to be thrown for every unimplemented endpoint. I'm afraid there is an infinite number of such cases...