DATAREST-1387 - DelegatingHandlerMapping now handles HttpMediaTypeNotSupportedException.

This commit is contained in:
Oliver Drotbohm
2019-06-05 23:38:35 +02:00
parent a9b3a765c2
commit 20a6d36e13
2 changed files with 10 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest;
import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.servlet.HandlerExecutionChain;
@@ -30,7 +31,7 @@ import org.springframework.web.servlet.HandlerMapping;
/**
* A {@link HandlerMapping} that considers a {@link List} of delegates. It will keep on traversing the delegates in case
* an {@link HttpMediaTypeNotAcceptableException} is thrown while trying to lookup the handler on a particular delegate.
*
*
* @author Oliver Gierke
* @soundtrack Benny Greb - Stabila (Moving Parts)
*/
@@ -40,7 +41,7 @@ public class DelegatingHandlerMapping implements HandlerMapping, Ordered {
/**
* Creates a new {@link DelegatingHandlerMapping} for the given delegates.
*
*
* @param delegates must not be {@literal null}.
*/
public DelegatingHandlerMapping(List<HandlerMapping> delegates) {
@@ -50,7 +51,7 @@ public class DelegatingHandlerMapping implements HandlerMapping, Ordered {
this.delegates = delegates;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
@@ -59,7 +60,7 @@ public class DelegatingHandlerMapping implements HandlerMapping, Ordered {
return Ordered.LOWEST_PRECEDENCE - 100;
}
/*
/*
* (non-Javadoc)
* @see org.springframework.web.servlet.HandlerMapping#getHandler(javax.servlet.http.HttpServletRequest)
*/
@@ -78,6 +79,8 @@ public class DelegatingHandlerMapping implements HandlerMapping, Ordered {
return result;
}
} catch (HttpMediaTypeNotSupportedException o_O) {
exception = o_O;
} catch (HttpMediaTypeNotAcceptableException o_O) {
exception = o_O;
} catch (HttpRequestMethodNotSupportedException o_O) {

View File

@@ -28,6 +28,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.servlet.HandlerMapping;
@@ -44,7 +45,7 @@ public class DelegatingHandlerMappingUnitTests {
@Mock HandlerMapping first, second;
@Mock HttpServletRequest request;
@Test // DATAREST-490, DATAREST-522
@Test // DATAREST-490, DATAREST-522, DATAREST-1387
public void consultsAllHandlerMappingsAndThrowsExceptionEventually() throws Exception {
DelegatingHandlerMapping mapping = new DelegatingHandlerMapping(Arrays.asList(first, second));
@@ -52,6 +53,7 @@ public class DelegatingHandlerMappingUnitTests {
assertHandlerTriedButExceptionThrown(mapping, HttpMediaTypeNotAcceptableException.class);
assertHandlerTriedButExceptionThrown(mapping, HttpRequestMethodNotSupportedException.class);
assertHandlerTriedButExceptionThrown(mapping, UnsatisfiedServletRequestParameterException.class);
assertHandlerTriedButExceptionThrown(mapping, HttpMediaTypeNotSupportedException.class); // DATAREST-1387
}
@SuppressWarnings("unchecked")