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

com.jn.agileway.springboot.web.rest.exceptionhandler.HttpRequestMethodNotSupportedExceptionHandler Maven / Gradle / Ivy

package com.jn.agileway.springboot.web.rest.exceptionhandler;


import com.jn.agileway.http.rest.RestActionException;
import com.jn.agileway.http.rest.RestActionExceptions;
import com.jn.agileway.web.rest.AbstractServletRestActionExceptionHandler;
import com.jn.langx.http.rest.RestRespBody;
import com.jn.langx.util.Emptys;
import com.jn.langx.util.Strings;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestMethodNotSupportedException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@RestActionExceptions({
        @RestActionException(HttpRequestMethodNotSupportedException.class)
})
public class HttpRequestMethodNotSupportedExceptionHandler extends AbstractServletRestActionExceptionHandler {
    @Override
    public RestRespBody handle(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
        HttpRequestMethodNotSupportedException ex = (HttpRequestMethodNotSupportedException) exception;
        RestRespBody respBody = RestRespBody.error(405, "HTTP-405", null);
        if (Emptys.isNotEmpty(ex.getSupportedMethods())) {
            String supportedMethods = Strings.join(", ", ex.getSupportedMethods());
            response.setHeader("Allow", supportedMethods);
            respBody.setErrorMessage("支持的HTTP Method有:" + supportedMethods);
        }

        return respBody;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy