0

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

wojtek1902
  • 503
  • 2
  • 10
  • 25
  • Could you please share a bit of code? If a resource cannot be found on the server, you should return a response with the `404` status code and a payload describing the error. If you actually want to indicate that something has not been implemented and the request cannot be handled, you should return a response with the `501` status code. – cassiomolin Jun 02 '17 at 10:37
  • _I use javax package._ Do you mean JAX-RS and one of its implementations? – cassiomolin Jun 02 '17 at 10:48
  • Yes, I use [javax.ws.rs](https://docs.oracle.com/javaee/7/api/javax/ws/rs/package-summary.html) – wojtek1902 Jun 02 '17 at 10:53
  • I added some code snippets. – wojtek1902 Jun 02 '17 at 11:04
  • See my [answer](https://stackoverflow.com/a/44327321/1426227). – cassiomolin Jun 02 '17 at 11:05

2 Answers2

0

Add the below method in your controller

@ExceptionHandler(ResourceNotFoundException.class)
        @ResponseStatus(HttpStatus.NOT_FOUND)
        public String handleResourceNotFoundException() {
            return "page not found";
        }

and below class to your code:

public class ResourceNotFoundException extends RuntimeException { }

Instead of returning page not found string, you can return a page by specifying some model in the method.

R Dhaval
  • 536
  • 1
  • 9
  • 21
  • The question is quite broad regarding the framework the OP is using. You shouldn't assume the OP is using Spring. – cassiomolin Jun 02 '17 at 10:45
  • I'm not using Spring unfortunately. – wojtek1902 Jun 02 '17 at 10:51
  • OP is not responding to your question *Could you please share a bit of code?* He/she is may be thinking that we know about code. so I just assume that he was using something like this . :D peace.. @CássioMazzochiMolin – R Dhaval Jun 02 '17 at 10:53
0

With JAX-RS, you can define an ExceptionMapper to map an exception to a Response:

@Provider
public class NotImplementedExceptionMapper 
       implements ExceptionMapper<NotImplementedException> {

    @Override
    public Response toResponse(EntityNotFoundException ex) {
        String html = ...
        return Response.status(501).entity(html).type(MediaType.TEXT_HTML).build();
    }
}

For more details on handling errors in JAX-RS, refer to this answer.


Picking the right status code:

  • If a resource cannot be found on the server, you should return a response with the 404 status code. It indicates a client error.
  • If you actually want to indicate that something has not been implemented and the request cannot be handled, you should return a response with the 501 status code. It indicates a server error.
Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Thank you. But as I understand, I have to map this implementation to the defined endpoint, don't I? And I want this code to be executed for EVERY unimplemented endpoint. – wojtek1902 Jun 02 '17 at 11:11
  • @user2905035 See my updated answer. `ExceptionMapper` is what you are looking for. It allows you to map an exception to a `Response`. – cassiomolin Jun 02 '17 at 11:17