rns true The following will also return true
GET / Accept: */ ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); assert matcher.matches(request) == true // returns true
Ignoring Media Types
Sometimes you may want to ignore certain types of media types. For example, you may want to match on "application/json" but ignore "*/" sent by a web browser.
GET / Accept: */ ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); assert matcher.matches(request) == false // returns false
GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setIgnoredMediaTypes(Collections.singleton(MediaType.ALL)); assert matcher.matches(request) == true // returns true
Exact media type comparison
By default as long as the {@link MediaType} discovered by{@link ContentNegotiationStrategy} returns true for{@link MediaType#isCompatibleWith(MediaType)} on the matchingMediaTypes, theresult of the match is true. However, sometimes you may want to perform an exact match. This can be done with the following examples:
GET / Accept: application/json ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == true // returns true
GET / Accept: application/ ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == false // returns false
GET / Accept: */ ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy() MediaTypeRequestMatcher matcher = new MediaTypeRequestMatcher(negotiationStrategy, MediaType.APPLICATION_JSON); matcher.setUseEquals(true); assert matcher.matches(request) == false // returns false
@author Rob Winch
@since 3.2