List of usage examples for org.springframework.web HttpRequestMethodNotSupportedException getSupportedMethods
@Nullable
public String[] getSupportedMethods()
From source file:cz.jirutka.spring.exhandler.handlers.HttpRequestMethodNotSupportedExceptionHandler.java
@Override protected HttpHeaders createHeaders(HttpRequestMethodNotSupportedException ex, HttpServletRequest req) { HttpHeaders headers = super.createHeaders(ex, req); if (!isEmpty(ex.getSupportedMethods())) { headers.setAllow(ex.getSupportedHttpMethods()); }// w w w .jav a2 s. c om return headers; }
From source file:com.pkrete.locationservice.admin.controller.rest.v1.RestExceptionHandler.java
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ResponseBody/* ww w . j a va2 s.c om*/ 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; }
From source file:org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerSupport.java
/** * Customize the response for HttpRequestMethodNotSupportedException. * This method logs a warning, sets the "Allow" header, and delegates to * {@link #handleExceptionInternal(Exception, HttpHeaders, HttpStatus, WebRequest)}. * @param ex the exception/* w ww.ja va2 s . c o m*/ * @param headers the headers to be written to the response * @param status the selected response status * @param request the current request * @return an Object or {@code null} */ protected Object handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { pageNotFoundLogger.warn(ex.getMessage()); Set<HttpMethod> mediaTypes = new HashSet<HttpMethod>(); for (String value : ex.getSupportedMethods()) { mediaTypes.add(HttpMethod.valueOf(value)); } if (!mediaTypes.isEmpty()) { headers.setAllow(mediaTypes); } return handleExceptionInternal(ex, headers, status, request); }
From source file:org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.java
/** * Handle the case where no request handler method was found for the particular HTTP request method. * <p>The default implementation logs a warning, sends an HTTP 405 error, sets the "Allow" header, * and returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, * or the HttpRequestMethodNotSupportedException could be rethrown as-is. * @param ex the HttpRequestMethodNotSupportedException to be handled * @param request current HTTP request/* w w w . jav a 2 s . c o m*/ * @param response current HTTP response * @param handler the executed handler, or {@code null} if none chosen * at the time of the exception (for example, if multipart resolution failed) * @return an empty ModelAndView indicating the exception was handled * @throws IOException potentially thrown from response.sendError() */ protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException { pageNotFoundLogger.warn(ex.getMessage()); String[] supportedMethods = ex.getSupportedMethods(); if (supportedMethods != null) { response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", ")); } response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage()); return new ModelAndView(); }