0

I have this Controller

@Controller
@RequestMapping(value = "/v1.0/user")
public class UserController {

    @RequestMapping(value = "/findOne/{id}", method = RequestMethod.GET)
    public @ResponseBody String findOne(@PathVariable("id") Integer id) {
        log.info("findOne");
        return "found URL";
    }       

}

Which will match the URL: http://localhost:8080/v1.0/user/findOne/4

But if my path varibale is not correct: http://localhost:8080/v1.0/user/findOne/4A

The I get nothing. Not even an error. It's as if Spring swallowed the URL.

I added

@RequestMapping(value = "/.*", method = RequestMethod.GET)
public @ResponseBody String redirectEverythingOtherThanTest(){
    log.info("no url matched");
    return "badly formed URL for Users";
}

Again I get nothing. What I'm trying to accomplish is for each Controller to have a unique message when the URL does not match.

P_C
  • 185
  • 2
  • 11
  • Maybe this can help you. http://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page – Robert I Mar 30 '17 at 01:48
  • I think you must be getting 500, when you are running with `4A` pathVariable, since it will throw an exception while trying to create an Integer from it. Can you check the status code of the response? The thing is that your URL does match, since `{id}` will cover everything after `/findOne/`. It is better to make `id` a `String` in your controller, and validate the value before creating an `Integer` from it. – buræquete Mar 30 '17 at 01:51
  • Actually the first issue is that I have a ControllerAdvice and it extends ResponseEntityExceptionHandler which has a handle for BindException.class. I removed the ControllerAdvice and now I get a 400 error. But can I have the 400 mapped to a specific catch all method in the Controller? – P_C Mar 30 '17 at 02:01

1 Answers1

0

In the end in each of my Controller classes I added

@ExceptionHandler(Exception.class)
public @ResponseBody String handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    return e.getMessage();

}

And this will catch all exceptions and I can treat it as a catch all.

P_C
  • 185
  • 2
  • 11