Add web support for Yaml via Jackson
This commit adds support for application/yaml in MediaType and leverages jackson-dataformat-yaml in order to support Yaml in RestTemplate, RestClient and Spring MVC. See gh-32345
This commit is contained in:
committed by
Sébastien Deleuze
parent
246e4977a2
commit
6a8f0d6d7d
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -45,6 +45,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Sebastien Deleuze
|
||||
* @author Kazuki Shimizu
|
||||
* @author Sam Brannen
|
||||
* @author Hyoungjune Kim
|
||||
* @since 3.0
|
||||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-3.1.1.1">
|
||||
* HTTP 1.1: Semantics and Content, section 3.1.1.1</a>
|
||||
@@ -311,6 +312,16 @@ public class MediaType extends MimeType implements Serializable {
|
||||
*/
|
||||
public static final String APPLICATION_XML_VALUE = "application/xml";
|
||||
|
||||
/**
|
||||
* Public constant media type for {@code application/yaml}.
|
||||
*/
|
||||
public static final MediaType APPLICATION_YAML;
|
||||
|
||||
/**
|
||||
* A String equivalent of {@link MediaType#APPLICATION_YAML}.
|
||||
*/
|
||||
public static final String APPLICATION_YAML_VALE = "application/yaml";
|
||||
|
||||
/**
|
||||
* Public constant media type for {@code image/gif}.
|
||||
*/
|
||||
@@ -454,6 +465,7 @@ public class MediaType extends MimeType implements Serializable {
|
||||
APPLICATION_STREAM_JSON = new MediaType("application", "stream+json");
|
||||
APPLICATION_XHTML_XML = new MediaType("application", "xhtml+xml");
|
||||
APPLICATION_XML = new MediaType("application", "xml");
|
||||
APPLICATION_YAML = new MediaType("application", "yaml");
|
||||
IMAGE_GIF = new MediaType("image", "gif");
|
||||
IMAGE_JPEG = new MediaType("image", "jpeg");
|
||||
IMAGE_PNG = new MediaType("image", "png");
|
||||
|
||||
@@ -56,6 +56,7 @@ import com.fasterxml.jackson.dataformat.smile.SmileFactory;
|
||||
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -95,6 +96,7 @@ import org.springframework.util.xml.StaxUtils;
|
||||
* @author Juergen Hoeller
|
||||
* @author Tadaya Tsuyukubo
|
||||
* @author Eddú Meléndez
|
||||
* @author Hyoungjune Kim
|
||||
* @since 4.1.1
|
||||
* @see #build()
|
||||
* @see #configure(ObjectMapper)
|
||||
@@ -936,6 +938,15 @@ public class Jackson2ObjectMapperBuilder {
|
||||
return new Jackson2ObjectMapperBuilder().factory(new CborFactoryInitializer().create());
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a {@link Jackson2ObjectMapperBuilder} instance in order to
|
||||
* build a Yaml data format {@link ObjectMapper} instance.
|
||||
* @since 6.2
|
||||
*/
|
||||
public static Jackson2ObjectMapperBuilder yaml() {
|
||||
return new Jackson2ObjectMapperBuilder().factory(new YamlFactoryInitializer().create());
|
||||
}
|
||||
|
||||
|
||||
private static class XmlObjectMapperInitializer {
|
||||
|
||||
@@ -976,4 +987,11 @@ public class Jackson2ObjectMapperBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
private static class YamlFactoryInitializer {
|
||||
|
||||
public JsonFactory create() {
|
||||
return new YAMLFactory();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.converter.yaml;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
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">
|
||||
* the dedicated Jackson 2.x extension</a>.
|
||||
*
|
||||
* <p>By default, this converter supports the {@link MediaType#APPLICATION_YAML_VALE}
|
||||
* media type. This can be overridden by setting the {@link #setSupportedMediaTypes
|
||||
* supportedMediaTypes} property.
|
||||
*
|
||||
* <p>The default constructor uses the default configuration provided by
|
||||
* {@link Jackson2ObjectMapperBuilder}.
|
||||
*
|
||||
* @author Hyoungjune Kim
|
||||
* @since 6.2
|
||||
*/
|
||||
public class MappingJackson2YamlHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2YamlHttpMessageConverter} using the
|
||||
* default configuration provided by {@code Jackson2ObjectMapperBuilder}.
|
||||
*/
|
||||
public MappingJackson2YamlHttpMessageConverter() {
|
||||
this(Jackson2ObjectMapperBuilder.yaml().build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new {@code MappingJackson2YamlHttpMessageConverter} with a
|
||||
* custom {@link ObjectMapper} (must be configured with a {@code YAMLFactory}
|
||||
* instance).
|
||||
* <p>You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
|
||||
* @see Jackson2ObjectMapperBuilder#yaml()
|
||||
*/
|
||||
public MappingJackson2YamlHttpMessageConverter(ObjectMapper objectMapper) {
|
||||
super(objectMapper, MediaType.APPLICATION_YAML);
|
||||
Assert.isInstanceOf(YAMLFactory.class, objectMapper.getFactory(), "YAMLFactory required");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* The {@code ObjectMapper} must be configured with a {@code YAMLFactory} instance.
|
||||
*/
|
||||
@Override
|
||||
public void setObjectMapper(ObjectMapper objectMapper) {
|
||||
Assert.isInstanceOf(YAMLFactory.class, objectMapper.getFactory(), "YAMLFactory required");
|
||||
super.setObjectMapper(objectMapper);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Provides an HttpMessageConverter for the Yaml data format.
|
||||
*/
|
||||
@NonNullApi
|
||||
@NonNullFields
|
||||
package org.springframework.http.converter.yaml;
|
||||
|
||||
import org.springframework.lang.NonNullApi;
|
||||
import org.springframework.lang.NonNullFields;
|
||||
@@ -48,6 +48,7 @@ import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessag
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageConverter;
|
||||
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
||||
import org.springframework.http.converter.yaml.MappingJackson2YamlHttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -60,6 +61,7 @@ import org.springframework.web.util.UriTemplateHandler;
|
||||
* Default implementation of {@link RestClient.Builder}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Hyoungjune Kim
|
||||
* @since 6.1
|
||||
*/
|
||||
final class DefaultRestClientBuilder implements RestClient.Builder {
|
||||
@@ -86,6 +88,8 @@ final class DefaultRestClientBuilder implements RestClient.Builder {
|
||||
|
||||
private static final boolean jackson2CborPresent;
|
||||
|
||||
private static final boolean jackson2YamlPresent;
|
||||
|
||||
|
||||
static {
|
||||
ClassLoader loader = DefaultRestClientBuilder.class.getClassLoader();
|
||||
@@ -101,6 +105,7 @@ final class DefaultRestClientBuilder implements RestClient.Builder {
|
||||
kotlinSerializationJsonPresent = ClassUtils.isPresent("kotlinx.serialization.json.Json", loader);
|
||||
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", loader);
|
||||
jackson2CborPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.cbor.CBORFactory", loader);
|
||||
jackson2YamlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.yaml.YAMLFactory", loader);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -394,6 +399,9 @@ final class DefaultRestClientBuilder implements RestClient.Builder {
|
||||
if (jackson2CborPresent) {
|
||||
this.messageConverters.add(new MappingJackson2CborHttpMessageConverter());
|
||||
}
|
||||
if (jackson2YamlPresent) {
|
||||
this.messageConverters.add(new MappingJackson2YamlHttpMessageConverter());
|
||||
}
|
||||
}
|
||||
return this.messageConverters;
|
||||
}
|
||||
|
||||
@@ -66,6 +66,7 @@ import org.springframework.http.converter.smile.MappingJackson2SmileHttpMessageC
|
||||
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
|
||||
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
|
||||
import org.springframework.http.converter.yaml.MappingJackson2YamlHttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -108,6 +109,7 @@ import org.springframework.web.util.UriTemplateHandler;
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @author Sebastien Deleuze
|
||||
* @author Hyoungjune Kim
|
||||
* @since 3.0
|
||||
* @see HttpMessageConverter
|
||||
* @see RequestCallback
|
||||
@@ -128,6 +130,8 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
|
||||
private static final boolean jackson2CborPresent;
|
||||
|
||||
private static final boolean jackson2YamlPresent;
|
||||
|
||||
private static final boolean gsonPresent;
|
||||
|
||||
private static final boolean jsonbPresent;
|
||||
@@ -149,6 +153,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
jackson2XmlPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.xml.XmlMapper", classLoader);
|
||||
jackson2SmilePresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.smile.SmileFactory", classLoader);
|
||||
jackson2CborPresent = ClassUtils.isPresent("com.fasterxml.jackson.dataformat.cbor.CBORFactory", 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);
|
||||
@@ -222,6 +227,10 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
|
||||
this.messageConverters.add(new KotlinSerializationCborHttpMessageConverter());
|
||||
}
|
||||
|
||||
if (jackson2YamlPresent) {
|
||||
this.messageConverters.add(new MappingJackson2YamlHttpMessageConverter());
|
||||
}
|
||||
|
||||
updateErrorHandlerConverters();
|
||||
this.uriTemplateHandler = initUriTemplateHandler();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user