本文整理汇总了Java中org.springframework.http.MediaType.parseMediaType方法的典型用法代码示例。如果您正苦于以下问题:Java MediaType.parseMediaType方法的具体用法?Java MediaType.parseMediaType怎么用?Java MediaType.parseMediaType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.http.MediaType
的用法示例。
在下文中一共展示了MediaType.parseMediaType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: isFormContentType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
private boolean isFormContentType(HttpServletRequest request) {
String contentType = request.getContentType();
if (contentType != null) {
try {
MediaType mediaType = MediaType.parseMediaType(contentType);
return (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType));
}
catch (IllegalArgumentException ex) {
return false;
}
}
else {
return false;
}
}
示例2: handleNoMatch
import org.springframework.http.MediaType; //导入方法依赖的package包/类
/**
* Look up the given extension via {@link ServletContext#getMimeType(String)}
* and if that doesn't help, delegate to the parent implementation.
*/
@Override
protected MediaType handleNoMatch(NativeWebRequest webRequest, String extension) {
MediaType mediaType = null;
if (this.servletContext != null) {
String mimeType = this.servletContext.getMimeType("file." + extension);
if (StringUtils.hasText(mimeType)) {
mediaType = MediaType.parseMediaType(mimeType);
}
}
if (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) {
MediaType superMediaType = super.handleNoMatch(webRequest, extension);
if (superMediaType != null) {
mediaType = superMediaType;
}
}
return mediaType;
}
示例3: checkContentType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
public void checkContentType(String contentType) {
try {
MediaType media = MediaType.parseMediaType(contentType);
// TODO improve the logic here
if (media.getSubtype() != null && !media.getSubtype().contains("xml") && !media.getSubtype().contains("fhir") && !media.getSubtype().contains("json") && !media.getSubtype().contains("plain")) {
log.info("Unsupported media type: " + contentType);
throw new InvalidRequestException("Unsupported media type: sub " + contentType);
} else {
if (!contentType.contains("xml") && !contentType.contains("json")) {
log.info("Unsupported media type: " + contentType);
throw new InvalidRequestException("Unsupported media type: content " + contentType);
}
}
} catch (InvalidMediaTypeException e) {
log.info("Unsupported media type: " + contentType);
throw new InvalidRequestException("Unsupported media type: mime " + contentType);
}
}
示例4: getContentInfo
import org.springframework.http.MediaType; //导入方法依赖的package包/类
/**
* Returns the basic content info from the request.
* @param req WebScriptRequest
* @return BasicContentInfo
*/
private BasicContentInfo getContentInfo(WebScriptRequest req) {
String encoding = "UTF-8";
String contentType = MimetypeMap.MIMETYPE_BINARY;
if (StringUtils.isNotEmpty(req.getContentType()))
{
MediaType media = MediaType.parseMediaType(req.getContentType());
contentType = media.getType()+'/'+media.getSubtype();
if (media.getCharSet() != null)
{
encoding = media.getCharSet().toString();
}
}
return new ContentInfoImpl(contentType, encoding, -1, Locale.getDefault());
}
示例5: getDefaultContentType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
@Override
protected MediaType getDefaultContentType(Resource resource) {
MediaType contentType = null;
if (resource instanceof WxMediaResource) {
contentType = ((WxMediaResource) resource).getContentType();
}
if (contentType == null && servletContext != null && resource.getFilename() != null) {
String mimeType = servletContext.getMimeType(resource.getFilename());
if (StringUtils.hasText(mimeType)) {
contentType = MediaType.parseMediaType(mimeType);
}
}
if (contentType != null) {
return contentType;
}
return super.getDefaultContentType(resource);
}
示例6: selectHeaderAccept
import org.springframework.http.MediaType; //导入方法依赖的package包/类
/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
* otherwise use all of them (joining into a string)
*
* @param accepts The accepts array to select from
* @return List The list of MediaTypes to use for the Accept header
*/
public List<MediaType> selectHeaderAccept(String[] accepts) {
if (accepts.length == 0) {
return null;
}
for (String accept : accepts) {
MediaType mediaType = MediaType.parseMediaType(accept);
if (isJsonMime(mediaType)) {
return Collections.singletonList(mediaType);
}
}
return MediaType.parseMediaTypes(StringUtils.arrayToCommaDelimitedString(accepts));
}
示例7: selectHeaderContentType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
/**
* Select the Content-Type header's value from the given array:
* if JSON exists in the given array, use it;
* otherwise use the first one of the array.
*
* @param contentTypes The Content-Type array to select from
* @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used.
*/
public MediaType selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) {
return MediaType.APPLICATION_JSON;
}
for (String contentType : contentTypes) {
MediaType mediaType = MediaType.parseMediaType(contentType);
if (isJsonMime(mediaType)) {
return mediaType;
}
}
return MediaType.parseMediaType(contentTypes[0]);
}
示例8: YamlJackson2HttpMessageConverter
import org.springframework.http.MediaType; //导入方法依赖的package包/类
YamlJackson2HttpMessageConverter() {
super(new YAMLMapper().enable(Feature.MINIMIZE_QUOTES), MediaType.parseMediaType("application/x-yaml"));
this.getObjectMapper().configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
this.getObjectMapper().configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
this.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
this.getObjectMapper().setSerializationInclusion(Include.NON_NULL);
}
示例9: determineEncoding
import org.springframework.http.MediaType; //导入方法依赖的package包/类
private String determineEncoding(String contentTypeHeader, String defaultEncoding) {
if (!StringUtils.hasText(contentTypeHeader)) {
return defaultEncoding;
}
MediaType contentType = MediaType.parseMediaType(contentTypeHeader);
Charset charset = contentType.getCharSet();
return (charset != null ? charset.name() : defaultEncoding);
}
示例10: getMediaType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
public static MediaType getMediaType(Resource resource) {
if(resource.getFilename() == null) {
return null;
}
String mediaType = fileTypeMap.getContentType(resource.getFilename());
return (StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null);
}
示例11: getHeaders
import org.springframework.http.MediaType; //导入方法依赖的package包/类
@Override
public HttpHeaders getHeaders() {
if (this.headers == null) {
this.headers = new HttpHeaders();
for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) {
String headerName = (String) headerNames.nextElement();
for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
headerValues.hasMoreElements();) {
String headerValue = (String) headerValues.nextElement();
this.headers.add(headerName, headerValue);
}
}
// HttpServletRequest exposes some headers as properties: we should include those if not already present
if (this.headers.getContentType() == null && this.servletRequest.getContentType() != null) {
MediaType contentType = MediaType.parseMediaType(this.servletRequest.getContentType());
this.headers.setContentType(contentType);
}
if (this.headers.getContentType() != null && this.headers.getContentType().getCharSet() == null &&
this.servletRequest.getCharacterEncoding() != null) {
MediaType oldContentType = this.headers.getContentType();
Charset charSet = Charset.forName(this.servletRequest.getCharacterEncoding());
Map<String, String> params = new HashMap<String, String>(oldContentType.getParameters());
params.put("charset", charSet.toString());
MediaType newContentType = new MediaType(oldContentType.getType(), oldContentType.getSubtype(), params);
this.headers.setContentType(newContentType);
}
if (this.headers.getContentLength() == -1 && this.servletRequest.getContentLength() != -1) {
this.headers.setContentLength(this.servletRequest.getContentLength());
}
}
return this.headers;
}
示例12: parseMediaType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
private MediaType parseMediaType(String contentType) {
try {
return MediaType.parseMediaType(contentType);
} catch (InvalidMediaTypeException ex) {
throw new ContentTypeInvalidException();
}
}
示例13: YamlJackson2HttpMessageConverter
import org.springframework.http.MediaType; //导入方法依赖的package包/类
YamlJackson2HttpMessageConverter() {
super(new YAMLMapper(), MediaType.parseMediaType("application/yaml"));
}
示例14: JsonJackson2HttpMessageConverter
import org.springframework.http.MediaType; //导入方法依赖的package包/类
JsonJackson2HttpMessageConverter() {
super(Json.mapper(), MediaType.parseMediaType("application/json"));
}
示例15: getMediaType
import org.springframework.http.MediaType; //导入方法依赖的package包/类
public static MediaType getMediaType(String filename) {
String mediaType = fileTypeMap.getContentType(filename);
return (StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null);
}