0

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.

3
  • An alternative would be to create a dummy GET method and have it do as you want. Not perfect but it's very unclear why you want to have it be handled at all. Commented Feb 2, 2024 at 3:29
  • what is want is just to not display the error in the screen whenever user access any api url in the application Commented Feb 2, 2024 at 3:39
  • If such is the case, need to add dummy GET handler in controller Commented Feb 2, 2024 at 4:33

1 Answer 1

0

In Spring Boot 3.2, this code works perfectly:

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> exceptionHandler(HttpRequestMethodNotSupportedException ex) {
        return new ResponseEntity<>("", HttpStatus.METHOD_NOT_ALLOWED);
    }
}

If your project doesn't pick GlobalExceptionHandler, it means that there are discovery problems or you have another ExceptionHandler bean somehow overrides that handler.
Make sure you updated project to the latest versions and GlobalExceptionHandler class in package that is discoverable for spring.

For example if app defined like that:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

Make sure you put GlobalExceptionHandler class into com.example.demo package or it's sub packages.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.