美文网首页
SpringBoot问题 - 引入jackson-datafor

SpringBoot问题 - 引入jackson-datafor

作者: 鱼da王 | 来源:发表于2019-11-06 15:07 被阅读0次

1. 现象

如题

2. 原因

public abstract class AbstractMessageConverterMethodProcessor extends AbstractMessageConverterMethodArgumentResolver
        implements HandlerMethodReturnValueHandler {
        /**
     * Writes the given return type to the given output message.
     * @param value the value to write to the output message
     * @param returnType the type of the value
     * @param inputMessage the input messages. Used to inspect the {@code Accept} header.
     * @param outputMessage the output message to write to
     * @throws IOException thrown in case of I/O errors
     * @throws HttpMediaTypeNotAcceptableException thrown when the conditions indicated
     * by the {@code Accept} header on the request cannot be met by the message converters
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
            ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
            throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {

        ... 省略部分代码
      
        List<MediaType> mediaTypesToUse;

        MediaType contentType = outputMessage.getHeaders().getContentType();
        if (contentType != null && contentType.isConcrete()) {
            mediaTypesToUse = Collections.singletonList(contentType);
        }
        else {
            HttpServletRequest request = inputMessage.getServletRequest();
      // 从request请求头中,获取accept变量的value
            List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(request);
      // 在RequestMapping中获取 produces的value
            List<MediaType> producibleMediaTypes = getProducibleMediaTypes(request, valueType, declaredType);

            if (outputValue != null && producibleMediaTypes.isEmpty()) {
                throw new HttpMessageNotWritableException(
                        "No converter found for return value of type: " + valueType);
            }
            mediaTypesToUse = new ArrayList<>();
      
      // 两个List进行匹配选择可用的返回类型
            for (MediaType requestedType : requestedMediaTypes) {
                for (MediaType producibleType : producibleMediaTypes) {
                    if (requestedType.isCompatibleWith(producibleType)) {
                        mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType));
                    }
                }
            }
            if (mediaTypesToUse.isEmpty()) {
                if (outputValue != null) {
                    throw new HttpMediaTypeNotAcceptableException(producibleMediaTypes);
                }
                return;
            }
            MediaType.sortBySpecificityAndQuality(mediaTypesToUse);
        }

    // 选择最终的返回数据类型
        MediaType selectedMediaType = null;
        for (MediaType mediaType : mediaTypesToUse) {
            if (mediaType.isConcrete()) {
                selectedMediaType = mediaType;
                break;
            }
            else if (mediaType.equals(MediaType.ALL) || mediaType.equals(MEDIA_TYPE_APPLICATION)) {
                selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
                break;
            }
        }

        if (selectedMediaType != null) {
            selectedMediaType = selectedMediaType.removeQualityValue();
            for (HttpMessageConverter<?> converter : this.messageConverters) {
                GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
                        (GenericHttpMessageConverter<?>) converter : null);
                if (genericConverter != null ?
                        ((GenericHttpMessageConverter) converter).canWrite(declaredType, valueType, selectedMediaType) :
                        converter.canWrite(valueType, selectedMediaType)) {
                    outputValue = getAdvice().beforeBodyWrite(outputValue, returnType, selectedMediaType,
                            (Class<? extends HttpMessageConverter<?>>) converter.getClass(),
                            inputMessage, outputMessage);
                    if (outputValue != null) {
                        addContentDispositionHeader(inputMessage, outputMessage);
                        if (genericConverter != null) {
                            genericConverter.write(outputValue, declaredType, selectedMediaType, outputMessage);
                        }
                        else {
                            ((HttpMessageConverter) converter).write(outputValue, selectedMediaType, outputMessage);
                        }
                        if (logger.isDebugEnabled()) {
                            logger.debug("Written [" + outputValue + "] as \"" + selectedMediaType +
                                    "\" using [" + converter + "]");
                        }
                    }
                    return;
                }
            }
        }

        if (outputValue != null) {
            throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
        }
    }
}

返回xml原因:requestedMediaTypes 在Request的accept中没有值。而producibleMediaTypes中返回类型排序是 application/xml 排第一位, 在 application/json 之前。因此最终匹配的时候,返回了 application/xml

3. 解决办法

  • 方法一:设置默认contentType, 通过 implements WebMvcconfigurer

    @Configuration
    public class WebInterceptorAdapter implements WebMvcConfigurer {
      @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            configurer.defaultContentType(MediaType.APPLICATION_JSON, MediaType.TEXT_XML, MediaType.APPLICATION_XML);
        }
    }
    
  • 方法二:由于我的项目重复引用了fastxml,去掉一个也恢复了

4. 关联问题

问题

​ 在上面的问题中,第一次只设定了 configurer.defaultContentType(MediaType.APPLICATION_JSON), 出现了问题,如下:

Could not find acceptable representation

Spring 不知道接受的类型是什么,因此你需要告诉Spring。

解决方案

  • 通过RequestMapping中参数consumer告诉Spring。

  • 设置默认contentType,设置很全。

    configurer.defaultContentType(MediaType.APPLICATION_JSON, MediaType.TEXT_XML, MediaType.APPLICATION_XML);

相关阅读*

*https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

相关文章

网友评论

      本文标题:SpringBoot问题 - 引入jackson-datafor

      本文链接:https://www.haomeiwen.com/subject/pzpubctx.html