All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.naharoo.commons.mstoolkit.rest.exceptionhandler.HttpRequestMethodNotSupportedExceptionHandler Maven / Gradle / Ivy

There is a newer version: 0.7.12
Show newest version
package com.naharoo.commons.mstoolkit.rest.exceptionhandler;

import com.naharoo.commons.mstoolkit.exceptions.CommonIssueType;
import com.naharoo.commons.mstoolkit.exceptions.IssueType;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;

@ControllerAdvice
@ConditionalOnProperty(prefix = "ms-toolkit.rest-exception-handler.handlers.enabled", name = "REQUEST_METHOD_NOT_SUPPORTED", havingValue = "true", matchIfMissing = true)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(name = "org.springframework.web.HttpRequestMethodNotSupportedException")
public class HttpRequestMethodNotSupportedExceptionHandler extends AbstractExceptionHandler {

  /**
   * Handle org.springframework.web.HttpRequestMethodNotSupportedException. Thrown when a request handler does not support a
   * specific request method.
   */
  @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  public ResponseEntity handle(
          final HttpRequestMethodNotSupportedException exception,
          final HttpServletRequest request
  ) {
    final HttpServletRequestInfoBuilder infoBuilder = HttpServletRequestInfoBuilder.newInstance(
            request);
    logTrace(exception, infoBuilder);

    final IssueType type = CommonIssueType.REQUEST_METHOD_NOT_SUPPORTED;
    final int statusCode = type.statusCode();

    final ResponseEntity response = ResponseEntity
            .status(statusCode)
            .body(new ApiErrorResponse(
                    statusCode,
                    singleton(type),
                    singletonList(exception.getLocalizedMessage()),
                    LocalDateTime.now()
            ));

    logInfo(exception, infoBuilder);
    return response;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy