Polish Yaml support

Closes gh-32345
This commit is contained in:
Sébastien Deleuze
2024-03-11 09:11:40 +01:00
parent 6a8f0d6d7d
commit 5ee11fb1b3
4 changed files with 15 additions and 5 deletions

View File

@@ -314,13 +314,15 @@ public class MediaType extends MimeType implements Serializable {
/**
* Public constant media type for {@code application/yaml}.
* @since 6.2
*/
public static final MediaType APPLICATION_YAML;
/**
* A String equivalent of {@link MediaType#APPLICATION_YAML}.
* @since 6.2
*/
public static final String APPLICATION_YAML_VALE = "application/yaml";
public static final String APPLICATION_YAML_VALUE = "application/yaml";
/**
* Public constant media type for {@code image/gif}.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@ import org.springframework.http.converter.protobuf.KotlinSerializationProtobufHt
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.http.converter.yaml.MappingJackson2YamlHttpMessageConverter;
import org.springframework.util.ClassUtils;
/**
@@ -47,6 +48,8 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv
private static final boolean jackson2SmilePresent;
private static final boolean jackson2YamlPresent;
private static final boolean gsonPresent;
private static final boolean jsonbPresent;
@@ -64,6 +67,7 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader);
jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
jackson2YamlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.yaml.YAMLFactory", classLoader);
gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader);
jsonbPresent = ClassUtils.isPresent("jakarta.json.bind.Jsonb", classLoader);
kotlinSerializationCborPresent = ClassUtils.isPresent("kotlinx.serialization.cbor.Cbor", classLoader);
@@ -99,6 +103,10 @@ public class AllEncompassingFormHttpMessageConverter extends FormHttpMessageConv
addPartConverter(new MappingJackson2SmileHttpMessageConverter());
}
if (jackson2YamlPresent) {
addPartConverter(new MappingJackson2YamlHttpMessageConverter());
}
if (kotlinSerializationCborPresent) {
addPartConverter(new KotlinSerializationCborHttpMessageConverter());
}

View File

@@ -27,10 +27,10 @@ import org.springframework.util.Assert;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter
* HttpMessageConverter} that can read and write the <a href="https://yaml.io/">YAML</a>
* data format using <a href="https://github.com/FasterXML/jackson-dataformat-yaml/tree/master">
* data format using <a href="https://github.com/FasterXML/jackson-dataformats-text/tree/2.17/yaml">
* the dedicated Jackson 2.x extension</a>.
*
* <p>By default, this converter supports the {@link MediaType#APPLICATION_YAML_VALE}
* <p>By default, this converter supports the {@link MediaType#APPLICATION_YAML_VALUE}
* media type. This can be overridden by setting the {@link #setSupportedMediaTypes
* supportedMediaTypes} property.
*

View File

@@ -470,7 +470,7 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
defaultMediaTypes.put("cbor", MediaType.APPLICATION_CBOR_VALUE);
}
if (jackson2YamlPresent) {
defaultMediaTypes.put("yaml", MediaType.APPLICATION_YAML_VALE);
defaultMediaTypes.put("yaml", MediaType.APPLICATION_YAML_VALUE);
}
return defaultMediaTypes;
}