Example usage for org.springframework.web HttpRequestMethodNotSupportedException getMethod

List of usage examples for org.springframework.web HttpRequestMethodNotSupportedException getMethod

Introduction

In this page you can find the example usage for org.springframework.web HttpRequestMethodNotSupportedException getMethod.

Prototype

public String getMethod() 

Source Link

Document

Return the HTTP request method that caused the failure.

Usage

From source file:br.com.modoagil.asr.rest.support.RESTErrorHandler.java

/**
 * Manipula exceo para status HTTP {@code 405}
 *
 * @param ex//from w ww  . j av  a 2s.c  o  m
 *            {@link HttpRequestMethodNotSupportedException}
 * @return resposta ao cliente
 */
@ResponseBody
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Response<E> processHttpRequestMethodNotSupportedException(
        final HttpRequestMethodNotSupportedException ex) {
    this.logger.info(
            "handleHttpRequestMethodNotSupportedException - Catching: " + ex.getClass().getSimpleName(), ex);
    return new ResponseBuilder<E>().success(false).message(ex.getMethod() + " no suportado.")
            .status(HttpStatus.METHOD_NOT_ALLOWED).build();
}

From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody//from   www. j a  v a  2 s . c  o m
protected Map handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, WebRequest request,
        HttpServletResponse response) {
    // Get supported methods
    StringBuilder methods = new StringBuilder();
    for (int i = 0; i < ex.getSupportedMethods().length; i++) {
        methods.append(ex.getSupportedMethods()[i]);
        if (i < ex.getSupportedMethods().length - 1) {
            methods.append(", ");
        }
    }
    // Set response status
    response.setStatus(405);
    // Set Allow  header
    response.addHeader("Allow", methods.toString());
    // Create Map containing all the fields   
    Map errors = new HashMap<String, String>();
    // Set message arguments
    Object[] args = new Object[] { ex.getMethod(), methods };
    // Get message and set argument values
    String message = messageSource.getMessage("rest.http.request.method.not.supported", args, null);
    // Add error message
    errors.put("error", message);
    // Not authenticated -> return error
    return errors;
}