Example usage for org.springframework.web HttpRequestMethodNotSupportedException HttpRequestMethodNotSupportedException

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

Introduction

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

Prototype

public HttpRequestMethodNotSupportedException(String method, @Nullable String[] supportedMethods) 

Source Link

Document

Create a new HttpRequestMethodNotSupportedException.

Usage

From source file:com.monarchapis.driver.spring.rest.ApiErrorResponseEntityExceptionHandlerTest.java

@Test
public void testHttpRequestMethodNotSupported() {
    Set<String> allowedMethods = Sets.newLinkedHashSet(Lists.newArrayList("GET", "POST"));
    ResponseEntity<Object> response = //
            performTest(new HttpRequestMethodNotSupportedException("PUT", allowedMethods), //
                    400, "methodNotAllowed");
    HttpHeaders headers = response.getHeaders();
    assertEquals(1, headers.size());//from w  ww  .  ja  v  a2  s  . c o m
    Set<String> actualAllow = new HashSet<String>();

    for (HttpMethod method : headers.getAllow()) {
        actualAllow.add(method.name());
    }
    assertEquals(allowedMethods, actualAllow);

    allowedMethods = Collections.emptySet();
    response = //
            performTest(new HttpRequestMethodNotSupportedException("PUT", allowedMethods), //
                    400, "methodNotAllowed");
    headers = response.getHeaders();
    assertEquals(0, headers.size());
}

From source file:com.iflytek.edu.cloud.frame.spring.rest.ServiceMethodInfoHandlerMapping.java

/**
 * Iterate all RequestMappingInfos once again, look if any match by URL at
 * least and raise exceptions accordingly.
 * @throws HttpRequestMethodNotSupportedException if there are matches by URL
 * but not by HTTP method//from ww w. j  a  v a  2  s.c om
 * @throws HttpMediaTypeNotAcceptableException if there are matches by URL
 * but not by consumable/producible media types
 */
@Override
protected HandlerMethod handleNoMatch(Set<ServiceMethodInfo> requestMappingInfos, String lookupPath,
        HttpServletRequest request) throws ServletException {

    Set<String> allowedMethods = new LinkedHashSet<String>(4);

    Set<ServiceMethodInfo> patternMatches = new HashSet<ServiceMethodInfo>();
    Set<ServiceMethodInfo> patternAndMethodMatches = new HashSet<ServiceMethodInfo>();

    for (ServiceMethodInfo info : requestMappingInfos) {
        if (info.getServiceMethodCondition().getMatchingCondition(request) != null) {
            patternMatches.add(info);
            if (info.getMethodsCondition().getMatchingCondition(request) != null) {
                patternAndMethodMatches.add(info);
            } else {
                for (RequestMethod method : info.getMethodsCondition().getMethods()) {
                    allowedMethods.add(method.name());
                }
            }
        }
    }

    if (patternMatches.isEmpty()) {
        return null;
    } else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
        throw new HttpRequestMethodNotSupportedException(request.getMethod(), allowedMethods);
    } else {
        return null;
    }
}