大家好,又见面了,我是你们的朋友全栈君。
/**
* Tries to determine the content/media type of this HTTP request iff the HTTP “Content-Type”
* header was not explicitly set by the user, otherwise the user provided value is used. If the
* “Content-Type” HTTP header value is null, then the content/media/payload of this HTTP request
* is inspected to determine the content type.
*
* The simplest evaluation sets the content type to “application/x-www-form-urlencoded” if this is
* a POST or PUT HTTP request, unless any request parameter value is determined to have multiple
* parts, the the content type will be “multipart/form-data”.
*
*
* @param defaultContentType the default content/media type to use when the content type cannot be
* determined from this HTTP request.
* @return a MediaType for the value of the HTTP Content-Type header as determined from this HTTP
* request.
* @see #getHeaders()
* @see org.springframework.http.HttpHeaders#getContentType()
* @see org.springframework.http.MediaType
*/
protected MediaType determineContentType(final MediaType defaultContentType) {
MediaType contentType = getHeaders().getContentType();
// if the content type HTTP header was not explicitly set, try to determine the media type from
// the content body
// of the HTTP request
if (contentType == null) {
if (isPost() || isPut()) {
OUT: for (final String name : getParameters().keySet()) {
for (final Object value : getParameters().get(name)) {
if (value != null && !(value instanceof String)) {
contentType = MediaType.MULTIPART_FORM_DATA;
break OUT;
}
}
}
// since this is a POST/PUT HTTP request, default the content/media type to
// “application/x-www-form-urlencoded”
contentType = ObjectUtils.defaultIfNull(contentType, MediaType.APPLICATION_FORM_URLENCODED);
} else {
// NOTE the “Content-Type” HTTP header is not applicable to GET/DELETE and other methods of
// HTTP requests
// since there is typically no content (media/payload/request body/etc) to send. Any request
// parameters
// are encoded in the URL as query parameters.
}
}
return ObjectUtils.defaultIfNull(contentType, defaultContentType);
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/141436.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...