Java Code Examples for org.springframework.web.HttpRequestMethodNotSupportedException#getSupportedMethods()

The following examples show how to use org.springframework.web.HttpRequestMethodNotSupportedException#getSupportedMethods() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpRequestHandlerServlet.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	Assert.state(this.target != null, "No HttpRequestHandler available");

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 2
Source File: ExceptionControllerAdvice.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * @param
 * @return
 * @author GS
 * @description 错误请求方式异常  HttpRequestMethodNotSupportedException
 * @date 2018/2/28 17:32
 */
@ResponseBody
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public MessageResult httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
    ex.printStackTrace();
    log.info(">>>错误请求方式异常>>",ex);
    String methods = "";
    //支持的请求方式
    String[] supportedMethods = ex.getSupportedMethods();
    for (String method : supportedMethods) {
        methods += method;
    }
    MessageResult result = MessageResult.error("Request method " + ex.getMethod() + "  not supported !" +
            " supported method : " + methods + "!");
    return result;
}
 
Example 3
Source File: JeecgBootExceptionHandler.java    From jeecg-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * @Author 政辉
 * @param e
 * @return
 */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Result<?> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
	StringBuffer sb = new StringBuffer();
	sb.append("不支持");
	sb.append(e.getMethod());
	sb.append("请求方法,");
	sb.append("支持以下");
	String [] methods = e.getSupportedMethods();
	if(methods!=null){
		for(String str:methods){
			sb.append(str);
			sb.append("、");
		}
	}
	log.error(sb.toString(), e);
	//return Result.error("没有权限,请联系管理员授权");
	return Result.error(405,sb.toString());
}
 
Example 4
Source File: ExceptionControllerAdvice.java    From ZTuoExchange_framework with MIT License 6 votes vote down vote up
/**
 * @param
 * @return
 * @author GS
 * @description 错误请求方式异常  HttpRequestMethodNotSupportedException
 * @date 2018/2/28 17:32
 */
@ResponseBody
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
public MessageResult httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
    ex.printStackTrace();
    log.info(">>>错误请求方式异常>>",ex);
    String methods = "";
    //支持的请求方式
    String[] supportedMethods = ex.getSupportedMethods();
    for (String method : supportedMethods) {
        methods += method;
    }
    MessageResult result = MessageResult.error("Request method " + ex.getMethod() + "  not supported !" +
            " supported method : " + methods + "!");
    return result;
}
 
Example 5
Source File: HttpRequestHandlerServlet.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	Assert.state(this.target != null, "No HttpRequestHandler available");

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 6
Source File: JeecgBootExceptionHandler.java    From teaching with Apache License 2.0 6 votes vote down vote up
/**
 * @Author 政辉
 * @param e
 * @return
 */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Result<?> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
	StringBuffer sb = new StringBuffer();
	sb.append("不支持");
	sb.append(e.getMethod());
	sb.append("请求方法,");
	sb.append("支持以下");
	String [] methods = e.getSupportedMethods();
	if(methods!=null){
		for(String str:methods){
			sb.append(str);
			sb.append("、");
		}
	}
	log.error(sb.toString(), e);
	//return Result.error("没有权限,请联系管理员授权");
	return Result.error(405,sb.toString());
}
 
Example 7
Source File: JeecgBootExceptionHandler.java    From jeecg-boot with Apache License 2.0 6 votes vote down vote up
/**
 * @Author 政辉
 * @param e
 * @return
 */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Result<?> HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e){
	StringBuffer sb = new StringBuffer();
	sb.append("不支持");
	sb.append(e.getMethod());
	sb.append("请求方法,");
	sb.append("支持以下");
	String [] methods = e.getSupportedMethods();
	if(methods!=null){
		for(String str:methods){
			sb.append(str);
			sb.append("、");
		}
	}
	log.error(sb.toString(), e);
	//return Result.error("没有权限,请联系管理员授权");
	return Result.error(405,sb.toString());
}
 
Example 8
Source File: HttpRequestHandlerServlet.java    From icure-backend with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	LocaleContextHolder.setLocale(request.getLocale());
	try {
		if (httpRequestHandler != null) {
			this.httpRequestHandler.handleRequest(request, response);
		}
	} catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	} finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 9
Source File: HttpRequestHandlerServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 10
Source File: HttpRequestHandlerServlet.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 11
Source File: MethodNotAllowedAdviceTrait.java    From problem-spring-web with MIT License 6 votes vote down vote up
@API(status = INTERNAL)
@ExceptionHandler
default ResponseEntity<Problem> handleRequestMethodNotSupportedException(
        final HttpRequestMethodNotSupportedException exception,
        final NativeWebRequest request) {

    @WTF
    @Facepalm("Nullable arrays... great work from Spring :/")
    @Nullable final String[] methods = exception.getSupportedMethods();

    if (methods == null || methods.length == 0) {
        return create(Status.METHOD_NOT_ALLOWED, exception, request);
    }

    final HttpHeaders headers = new HttpHeaders();
    headers.setAllow(requireNonNull(exception.getSupportedHttpMethods()));

    return create(Status.METHOD_NOT_ALLOWED, exception, request, headers);
}
 
Example 12
Source File: DefaultHandlerExceptionResolver.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * 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
 * @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 {@link HttpServletResponse#sendError}
 */
protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

	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();
}
 
Example 13
Source File: DefaultHandlerExceptionResolver.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * 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
 * @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 {@link HttpServletResponse#sendError}
 */
protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
		HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException {

	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();
}
 
Example 14
Source File: DefaultHandlerExceptionResolver.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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
 * @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, 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();
}
 
Example 15
Source File: DefaultHandlerExceptionResolver.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * 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
 * @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, 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();
}