Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (10.2k points)

How do I get a Spring 3.0 controller to trigger a 404?

I have a controller with @RequestMapping(value = "/**", method = RequestMethod.GET) and for some URLs accessing the controller, I want the container to come up with a 404.

1 Answer

0 votes
by (46k points)

Since Spring 3.0 you also can throw an Exception declared with @ResponseStatus annotation:

@ResponseStatus(value = HttpStatus.NOT_FOUND)

public class ResourceNotFoundException extends RuntimeException {

    ...

}

@Controller

public class SomeController {

    @RequestMapping.....

    public void handleCall() {

        if (isFound()) {

            // whatever

        }

        else {

            throw new ResourceNotFoundException(); 

        }

    }

}

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 31, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Sep 24, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...