When i access the URL api in the browser, i get this error in the screen:
Request method 'GET' not supported
What i want is to completely remove this error when i access the url directly in the browser. i tried creating a exception handler logic to catch the error and display a blank text but it does not work
here is my code:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.HttpRequestMethodNotSupportedException;
@controllerAdvice
public class GlobalExceptionHandler{
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<String> handleMethodNotAllowedExceptionException(HttpRequestMethodNotSupportedException ex){
return new ResponseEntity<>("",HttpStatus.METHOD_NOT_ALLOWED);
}
}
Is there a way to remove this error from the screen? any help would be appreciated.
In Spring Boot 3.2, this code works perfectly:
If your project doesn't pick
GlobalExceptionHandler, it means that there are discovery problems or you have anotherExceptionHandlerbean somehow overrides that handler.Make sure you updated project to the latest versions and
GlobalExceptionHandlerclass in package that is discoverable for spring.For example if app defined like that:
Make sure you put
GlobalExceptionHandlerclass intocom.example.demopackage or it's sub packages.