I want the application layer to handle unmapped requests in SpringMVC

Asked 2 years ago, Updated 2 years ago, 161 views

Can SpringMVC pick up all unmapped requests?

For example,

@RequestMapping(value="/{otherwise:.+}", method=RequestMethod.GET)
public String otherwise() threads Exception {
    US>throw new TargetNotFoundException("The specified page could not be found.");
}


@ExceptionHandler (TargetNotFoundException.class)
@ResponseStatus (value=HttpStatus.OK)
public String HandleException (final TargetNotFoundExceptione,
                                HttpServletRequest request,
                                HttpServletResponse response) {
    LOGGER.error(e.getMessage());
    e.printStackTrace();
    request.setAttribute("message", e.getMessage());
    request.setAttribute("status", "404");
    return "error";
}

If so,

http://domain/{.+} unmapped requests can be controlled, but
The http://domain/{.+}/{.+} URL is processed in the servlet container.

I'm trying to do something that I can't handle by error code on web.xml, so I'd like to pick it up with the controller.
Also, I thought about how to redirect at the transport layer, but it's troublesome to think that I have to set it up for each web application, so if I can handle it at the application layer, that's better.

apache spring tomcat mvc

2022-09-30 21:12

1 Answers

@RequestMapping value is multiplied by an ant pattern, so
If you don't use PathVariable, I think /** is fine.

However, for all http://domain/ContextRoot/**, JavaScript, CSS, etc.
I can't solve it well.
For example,

@RequestMapping("/**")
@Cotroller
public class SomeController {
    @RequestMapping(value="/**")
    public String index() {
        return "index";
    }
}


/js/some.js and /css/style.css are also handled by the controller.


2022-09-30 21:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.