Top Description Inners Fields Constructors Methods
org.springframework.web.servlet.mvc.condition

public final Class ConsumesRequestCondition

extends AbstractRequestCondition<ConsumesRequestCondition>
Class Inheritance
Imports
java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .List, .Set, jakarta.servlet.http.HttpServletRequest, org.springframework.http.HttpHeaders, .InvalidMediaTypeException, .MediaType, org.springframework.lang.Nullable, org.springframework.util.CollectionUtils, .ObjectUtils, .StringUtils, org.springframework.web.bind.annotation.RequestMapping, org.springframework.web.cors.CorsUtils, org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.HeaderExpression

A logical disjunction (' || ') request condition to match a request's 'Content-Type' header to a list of media type expressions. Two kinds of media type expressions are supported, which are described in RequestMapping#consumes() and RequestMapping#headers() where the header name is 'Content-Type'. Regardless of which syntax is used, the semantics are the same.
Authors
Arjen Poutsma, Rossen Stoyanchev
Since
3.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
ConsumesRequestCondition.ConsumeMediaTypeExpression

Parses and matches a single media type expression to a request's 'Content-Type' header.

Field Summary

Modifier and TypeField and Description
private boolean
private static final ConsumesRequestCondition
private final List<ConsumesRequestCondition.ConsumeMediaTypeExpression>

Constructor Summary

AccessConstructor and Description
public
ConsumesRequestCondition(String...
expressions with the syntax described in RequestMapping#consumes(); if 0 expressions are provided, the condition will match to every request
consumes
)

Creates a new instance from 0 or more "consumes" expressions.

public
ConsumesRequestCondition(String[] consumes, String[]
as described in RequestMapping#headers()
headers
)

Creates a new instance with "consumes" and "header" expressions.

private
ConsumesRequestCondition(List<ConsumesRequestCondition.ConsumeMediaTypeExpression> expressions)

Private constructor for internal when creating matching conditions.

Method Summary

Modifier and TypeMethod and Description
public ConsumesRequestCondition
combine(ConsumesRequestCondition
the condition to combine with.
other
)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

Returns the "other" instance if it has any expressions; returns "this" instance otherwise.

public int
compareTo(ConsumesRequestCondition other, HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.compareTo.

Returns:

  • 0 if the two conditions have the same number of expressions
  • Less than 0 if "this" has more or more specific media type expressions
  • Greater than 0 if "other" has more or more specific media type expressions
public Set<MediaType>
getConsumableMediaTypes()

Returns the media types for this condition excluding negated expressions.

protected Collection<ConsumesRequestCondition.ConsumeMediaTypeExpression>
getContent()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Return the discrete items a request condition is composed of.

public Set<MediaTypeExpression>
getExpressions()

Return the contained MediaType expressions.

public ConsumesRequestCondition

Returns:

the same instance if the condition contains no expressions; or a new condition with matching expressions only; or null if no expressions match
getMatchingCondition
(HttpServletRequest
the current request
request
)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.getMatchingCondition.

Checks if any of the contained media type expressions match the given request 'Content-Type' header and returns an instance that is guaranteed to contain matching expressions only.

private List<ConsumesRequestCondition.ConsumeMediaTypeExpression>
protected String
getToStringInfix()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

The notation to use when printing discrete items of content.

private boolean
public boolean
isBodyRequired()

Return the setting for setBodyRequired(boolean).

public boolean
isEmpty()

Overrides org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.isEmpty.

Whether the condition has any media type expressions.

private static List<ConsumesRequestCondition.ConsumeMediaTypeExpression>
parseExpressions(String[] consumes, String[] headers)

public void
setBodyRequired(boolean
whether requests are expected to have a body
bodyRequired
)

Whether this condition should expect requests to have a body.

Inherited from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition:
equalshashCodetoString

Field Detail

bodyRequiredback to summary
private boolean bodyRequired
EMPTY_CONDITIONback to summary
private static final ConsumesRequestCondition EMPTY_CONDITION
expressionsback to summary
private final List<ConsumesRequestCondition.ConsumeMediaTypeExpression> expressions

Constructor Detail

ConsumesRequestConditionback to summary
public ConsumesRequestCondition(String... consumes)

Creates a new instance from 0 or more "consumes" expressions.

Parameters
consumes:String[]

expressions with the syntax described in RequestMapping#consumes(); if 0 expressions are provided, the condition will match to every request

ConsumesRequestConditionback to summary
public ConsumesRequestCondition(String[] consumes, String[] headers)

Creates a new instance with "consumes" and "header" expressions. "Header" expressions where the header name is not 'Content-Type' or have no header value defined are ignored. If 0 expressions are provided in total, the condition will match to every request

Parameters
consumes:String[]

as described in RequestMapping#consumes()

headers:String[]

as described in RequestMapping#headers()

ConsumesRequestConditionback to summary
private ConsumesRequestCondition(List<ConsumesRequestCondition.ConsumeMediaTypeExpression> expressions)

Private constructor for internal when creating matching conditions. Note the expressions List is neither sorted nor deep copied.

Method Detail

combineback to summary
public ConsumesRequestCondition combine(ConsumesRequestCondition other)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

Returns the "other" instance if it has any expressions; returns "this" instance otherwise. Practically that means a method-level "consumes" overrides a type-level "consumes" condition.

Parameters
other:ConsumesRequestCondition

Doc from org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

the condition to combine with.

Returns:ConsumesRequestCondition

Doc from org.springframework.web.servlet.mvc.condition.RequestCondition.combine.

a request condition instance that is the result of combining the two condition instances.

Annotations
@Override
compareToback to summary
public int compareTo(ConsumesRequestCondition other, HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.compareTo.

Returns:

  • 0 if the two conditions have the same number of expressions
  • Less than 0 if "this" has more or more specific media type expressions
  • Greater than 0 if "other" has more or more specific media type expressions

It is assumed that both instances have been obtained via getMatchingCondition(HttpServletRequest) and each instance contains the matching consumable media type expression only or is otherwise empty.

Annotations
@Override
getConsumableMediaTypesback to summary
public Set<MediaType> getConsumableMediaTypes()

Returns the media types for this condition excluding negated expressions.

getContentback to summary
protected Collection<ConsumesRequestCondition.ConsumeMediaTypeExpression> getContent()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getContent.

Return the discrete items a request condition is composed of.

For example URL patterns, HTTP request methods, param expressions, etc.

Returns:Collection<ConsumesRequestCondition.ConsumeMediaTypeExpression>

a collection of objects (never null)

Annotations
@Override
getExpressionsback to summary
public Set<MediaTypeExpression> getExpressions()

Return the contained MediaType expressions.

getMatchingConditionback to summary
public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request)

Implements org.springframework.web.servlet.mvc.condition.RequestCondition.getMatchingCondition.

Checks if any of the contained media type expressions match the given request 'Content-Type' header and returns an instance that is guaranteed to contain matching expressions only. The match is performed via MediaType#includes(MediaType).

Parameters
request:HttpServletRequest

the current request

Returns:ConsumesRequestCondition

the same instance if the condition contains no expressions; or a new condition with matching expressions only; or null if no expressions match

Annotations
@Override
@Nullable
getMatchingExpressionsback to summary
private List<ConsumesRequestCondition.ConsumeMediaTypeExpression> getMatchingExpressions(MediaType contentType)
Annotations
@Nullable
getToStringInfixback to summary
protected String getToStringInfix()

Implements abstract org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.getToStringInfix.

The notation to use when printing discrete items of content.

For example " || " for URL patterns or " && " for param expressions.

Annotations
@Override
hasBodyback to summary
private boolean hasBody(HttpServletRequest request)
isBodyRequiredback to summary
public boolean isBodyRequired()

Return the setting for setBodyRequired(boolean).

Since
5.2
isEmptyback to summary
public boolean isEmpty()

Overrides org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.isEmpty.

Whether the condition has any media type expressions.

Returns:boolean

Doc from org.springframework.web.servlet.mvc.condition.AbstractRequestCondition.isEmpty.

true if empty; false otherwise

Annotations
@Override
parseExpressionsback to summary
private static List<ConsumesRequestCondition.ConsumeMediaTypeExpression> parseExpressions(String[] consumes, String[] headers)
setBodyRequiredback to summary
public void setBodyRequired(boolean bodyRequired)

Whether this condition should expect requests to have a body.

By default this is set to true in which case it is assumed a request body is required and this condition matches to the "Content-Type" header or falls back on "Content-Type: application/octet-stream".

If set to false, and the request does not have a body, then this condition matches automatically, i.e. without checking expressions.

Parameters
bodyRequired:boolean

whether requests are expected to have a body

Since
5.2
org.springframework.web.servlet.mvc.condition back to summary

pack-priv Class ConsumesRequestCondition.ConsumeMediaTypeExpression

extends AbstractMediaTypeExpression
Class Inheritance

Parses and matches a single media type expression to a request's 'Content-Type' header.

Constructor Summary

AccessConstructor and Description
pack-priv
pack-priv
ConsumeMediaTypeExpression(MediaType mediaType, boolean negated)

Method Summary

Modifier and TypeMethod and Description
public final boolean
match(MediaType contentType)

Inherited from org.springframework.web.servlet.mvc.condition.AbstractMediaTypeExpression:
compareToequalsgetMediaTypehashCodeisNegatedmatchParameterstoString

Constructor Detail

ConsumeMediaTypeExpressionback to summary
pack-priv ConsumeMediaTypeExpression(String expression)
ConsumeMediaTypeExpressionback to summary
pack-priv ConsumeMediaTypeExpression(MediaType mediaType, boolean negated)

Method Detail

matchback to summary
public final boolean match(MediaType contentType)