0 votes
1 view
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 (46.1k 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
asked Oct 31 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...