• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java InvalidMediaTypeException类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.springframework.http.InvalidMediaTypeException的典型用法代码示例。如果您正苦于以下问题:Java InvalidMediaTypeException类的具体用法?Java InvalidMediaTypeException怎么用?Java InvalidMediaTypeException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



InvalidMediaTypeException类属于org.springframework.http包,在下文中一共展示了InvalidMediaTypeException类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: checkContentType

import org.springframework.http.InvalidMediaTypeException; //导入依赖的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);
    }
}
 
开发者ID:nhsconnect,项目名称:careconnect-reference-implementation,代码行数:20,代码来源:ServerInterceptor.java


示例2: resolveMediaTypes

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
 * {@inheritDoc}
 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header
 * cannot be parsed.
 */
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest request)
		throws HttpMediaTypeNotAcceptableException {

	String header = request.getHeader(HttpHeaders.ACCEPT);
	if (!StringUtils.hasText(header)) {
		return Collections.emptyList();
	}
	try {
		List<MediaType> mediaTypes = MediaType.parseMediaTypes(header);
		MediaType.sortBySpecificityAndQuality(mediaTypes);
		return mediaTypes;
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotAcceptableException(
				"Could not parse 'Accept' header [" + header + "]: " + ex.getMessage());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:24,代码来源:HeaderContentNegotiationStrategy.java


示例3: shouldFilter

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
@Override
public boolean shouldFilter() {
	RequestContext ctx = RequestContext.getCurrentContext();
	HttpServletRequest request = ctx.getRequest();
	String contentType = request.getContentType();
	// Don't use this filter on GET method
	if (contentType == null) {
		return false;
	}
	// Only use this filter for form data and only for multipart data in a
	// DispatcherServlet handler
	try {
		MediaType mediaType = MediaType.valueOf(contentType);
		return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)
				|| (isDispatcherServletRequest(request)
						&& MediaType.MULTIPART_FORM_DATA.includes(mediaType));
	}
	catch (InvalidMediaTypeException ex) {
		return false;
	}
}
 
开发者ID:spring-cloud,项目名称:spring-cloud-netflix,代码行数:22,代码来源:FormBodyWrapperFilter.java


示例4: isJsonMime

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
* Check if the given {@code String} is a JSON MIME.
* @param mediaType the input MediaType
* @return boolean true if the MediaType represents JSON, false otherwise
*/
public boolean isJsonMime(String mediaType) {
    try {
        return isJsonMime(MediaType.parseMediaType(mediaType));
    } catch (InvalidMediaTypeException e) {
    }
    return false;
}
 
开发者ID:jopache,项目名称:Settings,代码行数:13,代码来源:ApiClient.java


示例5: resolveMediaTypes

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
 * {@inheritDoc}
 * @throws HttpMediaTypeNotAcceptableException if the 'Accept' header cannot be parsed.
 */
@Override
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException {
	String acceptHeader = webRequest.getHeader(ACCEPT_HEADER);
	try {
		if (StringUtils.hasText(acceptHeader)) {
			List<MediaType> mediaTypes = MediaType.parseMediaTypes(acceptHeader);
			MediaType.sortBySpecificityAndQuality(mediaTypes);
			return mediaTypes;
		}
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotAcceptableException(
				"Could not parse accept header [" + acceptHeader + "]: " + ex.getMessage());
	}
	return Collections.emptyList();
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:21,代码来源:HeaderContentNegotiationStrategy.java


示例6: parseMediaType

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
private MediaType parseMediaType(String contentType) {
	try {
		return MediaType.parseMediaType(contentType);
	} catch (InvalidMediaTypeException ex) {
		throw new ContentTypeInvalidException();
	}
}
 
开发者ID:wesley-ramos,项目名称:spring-multitenancy,代码行数:8,代码来源:SimpleMediaType.java


示例7: matchMediaType

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
@Override
protected boolean matchMediaType(HttpServletRequest request) throws HttpMediaTypeNotSupportedException {
	try {
		MediaType contentType = StringUtils.hasLength(request.getContentType()) ?
				MediaType.parseMediaType(request.getContentType()) :
				MediaType.APPLICATION_OCTET_STREAM;
				return getMediaType().includes(contentType);
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotSupportedException(
				"Can't parse Content-Type [" + request.getContentType() + "]: " + ex.getMessage());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:14,代码来源:ConsumesRequestCondition.java


示例8: isCompatibleWith

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
 * Indicates whether the media type (content type) of the
 * given HTTP request is compatible with the given media type.
 * 
 * @param request the HTTP response.
 * @param mediaType the media type.
 */
public static boolean isCompatibleWith( HttpServletResponse response, MediaType mediaType )
{                
    try
    {
        String contentType = response.getContentType();
        
        return contentType != null && MediaType.parseMediaType( contentType ).isCompatibleWith( mediaType );
    }
    catch ( InvalidMediaTypeException ex )
    {
        return false;
    }
}
 
开发者ID:dhis2,项目名称:dhis2-core,代码行数:21,代码来源:ContextUtils.java


示例9: contentTypeAllowed

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
private boolean contentTypeAllowed(String contentType) {
    MediaType mediaType;
    try {
        mediaType = MediaType.parseMediaType(contentType);
    } catch (InvalidMediaTypeException e) {
        log.warn("Couldn't parse media type {}", contentType, e);
        return false;
    }
    for (MediaType el : attachmentTypesAllowed) {
        if (el.includes(mediaType)) {
            return true;
        }
    }
    return false;
}
 
开发者ID:as-ideas,项目名称:crowdsource,代码行数:16,代码来源:ProjectController.java


示例10: extractMapping

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
 * Searches {@link org.springframework.web.bind.annotation.RequestMapping RequestMapping}
 * annotation on the given method argument and extracts
 * If RequestMapping annotation is not found, NoRequestMappingFoundException is thrown.
 * {@link org.springframework.http.HttpMethod HttpMethod} type equivalent to
 * {@link org.springframework.web.bind.annotation.RequestMethod RequestMethod} type
 *
 * @param element AnnotatedElement object to be examined.
 * @return Mapping object
 */
Mapping extractMapping(AnnotatedElement element) {
  Annotation annotation = findMappingAnnotation(element);
  String[] urls;
  RequestMethod requestMethod;
  String consumes;

  if (annotation instanceof RequestMapping) {
    RequestMapping requestMapping = (RequestMapping) annotation;
    requestMethod = requestMapping.method().length == 0
        ? RequestMethod.GET : requestMapping.method()[0];
    urls = requestMapping.value();
    consumes = StringHelper.getFirstOrEmpty(requestMapping.consumes());

  } else if (annotation instanceof GetMapping) {

    requestMethod = RequestMethod.GET;
    urls = ((GetMapping) annotation).value();
    consumes = StringHelper.getFirstOrEmpty(((GetMapping) annotation).consumes());

  } else if (annotation instanceof PostMapping) {

    requestMethod = RequestMethod.POST;
    urls = ((PostMapping) annotation).value();
    consumes = StringHelper.getFirstOrEmpty(((PostMapping) annotation).consumes());

  } else if (annotation instanceof PutMapping) {

    requestMethod = RequestMethod.PUT;
    urls = ((PutMapping) annotation).value();
    consumes = StringHelper.getFirstOrEmpty(((PutMapping) annotation).consumes());

  } else if (annotation instanceof DeleteMapping) {

    requestMethod = RequestMethod.DELETE;
    urls = ((DeleteMapping) annotation).value();
    consumes = StringHelper.getFirstOrEmpty(((DeleteMapping) annotation).consumes());

  } else if (annotation instanceof PatchMapping) {

    requestMethod = RequestMethod.PATCH;
    urls = ((PatchMapping) annotation).value();
    consumes = StringHelper.getFirstOrEmpty(((PatchMapping) annotation).consumes());

  } else {
    throw new NoRequestMappingFoundException(element);
  }

  HttpMethod httpMethod = HttpMethod.resolve(requestMethod.name());
  String url = StringHelper.getFirstOrEmpty(urls);

  MediaType mediaType;
  try {
    mediaType =  MediaType.valueOf(consumes);
  } catch (InvalidMediaTypeException exception) {
    mediaType = MediaType.APPLICATION_JSON_UTF8;
  }

  return new Mapping(httpMethod, url, mediaType);

}
 
开发者ID:mental-party,项目名称:meparty,代码行数:71,代码来源:RestApiProxyInvocationHandler.java


示例11: handleNoMatch

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
 * Iterate all RequestMappingInfos once again, look if any match by URL at
 * least and raise exceptions accordingly.
 * @throws HttpRequestMethodNotSupportedException if there are matches by URL
 * but not by HTTP method
 * @throws HttpMediaTypeNotAcceptableException if there are matches by URL
 * but not by consumable/producible media types
 */
@Override
protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> requestMappingInfos,
		String lookupPath, HttpServletRequest request) throws ServletException {

	Set<String> allowedMethods = new LinkedHashSet<String>(4);

	Set<RequestMappingInfo> patternMatches = new HashSet<RequestMappingInfo>();
	Set<RequestMappingInfo> patternAndMethodMatches = new HashSet<RequestMappingInfo>();

	for (RequestMappingInfo info : requestMappingInfos) {
		if (info.getPatternsCondition().getMatchingCondition(request) != null) {
			patternMatches.add(info);
			if (info.getMethodsCondition().getMatchingCondition(request) != null) {
				patternAndMethodMatches.add(info);
			}
			else {
				for (RequestMethod method : info.getMethodsCondition().getMethods()) {
					allowedMethods.add(method.name());
				}
			}
		}
	}

	if (patternMatches.isEmpty()) {
		return null;
	}
	else if (patternAndMethodMatches.isEmpty() && !allowedMethods.isEmpty()) {
		throw new HttpRequestMethodNotSupportedException(request.getMethod(), allowedMethods);
	}

	Set<MediaType> consumableMediaTypes;
	Set<MediaType> producibleMediaTypes;
	List<String[]> paramConditions;

	if (patternAndMethodMatches.isEmpty()) {
		consumableMediaTypes = getConsumableMediaTypes(request, patternMatches);
		producibleMediaTypes = getProducibleMediaTypes(request, patternMatches);
		paramConditions = getRequestParams(request, patternMatches);
	}
	else {
		consumableMediaTypes = getConsumableMediaTypes(request, patternAndMethodMatches);
		producibleMediaTypes = getProducibleMediaTypes(request, patternAndMethodMatches);
		paramConditions = getRequestParams(request, patternAndMethodMatches);
	}

	if (!consumableMediaTypes.isEmpty()) {
		MediaType contentType = null;
		if (StringUtils.hasLength(request.getContentType())) {
			try {
				contentType = MediaType.parseMediaType(request.getContentType());
			}
			catch (InvalidMediaTypeException ex) {
				throw new HttpMediaTypeNotSupportedException(ex.getMessage());
			}
		}
		throw new HttpMediaTypeNotSupportedException(contentType, new ArrayList<MediaType>(consumableMediaTypes));
	}
	else if (!producibleMediaTypes.isEmpty()) {
		throw new HttpMediaTypeNotAcceptableException(new ArrayList<MediaType>(producibleMediaTypes));
	}
	else if (!CollectionUtils.isEmpty(paramConditions)) {
		throw new UnsatisfiedServletRequestParameterException(paramConditions, request.getParameterMap());
	}
	else {
		return null;
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:76,代码来源:RequestMappingInfoHandlerMapping.java


示例12: readWithMessageConverters

import org.springframework.http.InvalidMediaTypeException; //导入依赖的package包/类
/**
 * Creates the method argument value of the expected parameter type by reading
 * from the given HttpInputMessage.
 *
 * @param <T> the expected type of the argument value to be created
 * @param inputMessage the HTTP input message representing the current request
 * @param methodParam the method argument
 * @param targetType the type of object to create, not necessarily the same as
 * the method parameter type (e.g. for {@code HttpEntity<String>} method
 * parameter the target type is String)
 * @return the created method argument value
 * @throws IOException if the reading from the request fails
 * @throws HttpMediaTypeNotSupportedException if no suitable message converter is found
 */
@SuppressWarnings("unchecked")
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage,
		MethodParameter methodParam, Type targetType) throws IOException, HttpMediaTypeNotSupportedException {

	MediaType contentType;
	try {
		contentType = inputMessage.getHeaders().getContentType();
	}
	catch (InvalidMediaTypeException ex) {
		throw new HttpMediaTypeNotSupportedException(ex.getMessage());
	}

	if (contentType == null) {
		contentType = MediaType.APPLICATION_OCTET_STREAM;
	}

			Class<?> contextClass = methodParam.getDeclaringClass();
			Map<TypeVariable, Type> map = GenericTypeResolver.getTypeVariableMap(contextClass);
			Class<T> targetClass = (Class<T>) GenericTypeResolver.resolveType(targetType, map);

			for (HttpMessageConverter<?> converter : this.messageConverters) {
				if (converter instanceof GenericHttpMessageConverter) {
					GenericHttpMessageConverter genericConverter = (GenericHttpMessageConverter) converter;
					if (genericConverter.canRead(targetType, contextClass, contentType)) {
						if (logger.isDebugEnabled()) {
							logger.debug("Reading [" + targetType + "] as \"" +
									contentType + "\" using [" + converter + "]");
						}
						return genericConverter.read(targetType, contextClass, inputMessage);
					}
				}
				if (targetClass != null) {
					if (converter.canRead(targetClass, contentType)) {
						if (logger.isDebugEnabled()) {
							logger.debug("Reading [" + targetClass.getName() + "] as \"" +
									contentType + "\" using [" + converter + "]");
						}
						return ((HttpMessageConverter<T>) converter).read(targetClass, inputMessage);
					}
				}
			}

			throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
		}
 
开发者ID:deathspeeder,项目名称:class-guard,代码行数:59,代码来源:AbstractMessageConverterMethodArgumentResolver.java



注:本文中的org.springframework.http.InvalidMediaTypeException类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java BlackListInfo类代码示例发布时间:2022-05-21
下一篇:
Java HighlightDisplayKey类代码示例发布时间:2022-05-21
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap