1

I created web application in spring and handled exception mappings for 404 and 500.If system doesn't find any resources for requested URL it redirects into custom 404.jsp instead of regular 404 page.Everything works fine till have decided to add a webservice in my app.I have included one more controller as a webservice there is no view for this controller and this needs to be invoke through curl command.

User may get into change the curl script.If they changed the URL it should show 404 status code.But it returns the custom 404.jsp as a html response instead of status code.Because dispatcher servlet will takes all urls with /*.

How I can solve this issue?

Please share your suggestions.

sridhar
  • 303
  • 5
  • 26
  • please post how you configure your 404 custom page –  Nov 04 '14 at 09:01
  • You should have a look at these answer: http://stackoverflow.com/a/26634576/280244 - it shows how to have an other exception handler depending on the `Accept Type` header - I think you can adapt this, for your web service. – Ralph Nov 04 '14 at 09:25
  • I hope this can help you in getting what you want [https://stackoverflow.com/a/46029597/6941879](https://stackoverflow.com/a/46029597/6941879) – AbdusSalam Sep 04 '17 at 02:42

1 Answers1

1

Spring 3.2 introduced the @ControllerAdvice, and as mentioned in the documentation:

It is typically used to define @ExceptionHandler

That means you can use the @ControllerAdvice to assist your @Controller like the following:

@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ResponseStatus(HttpStatus.NOT_FOUND)  // 404
    @ExceptionHandler(Exception.class)
    public void handleNoTFound() {
        // Nothing to do
    }
}

For further details please refer to this tutorial and this answer.

ilja
  • 351
  • 2
  • 14
  • Thats good. But here some of the urls belongs to application for this system should show custom error page as a view. Remaining urls needs to act a webservice and it should return the status code when 404 occurs rather gives html content as a response – sridhar Nov 04 '14 at 10:14