001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.http.converter.xml;
018
019import java.nio.charset.StandardCharsets;
020
021import com.fasterxml.jackson.databind.ObjectMapper;
022import com.fasterxml.jackson.dataformat.xml.XmlMapper;
023
024import org.springframework.http.MediaType;
025import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
026import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
027import org.springframework.util.Assert;
028
029/**
030 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
031 * that can read and write XML using <a href="https://github.com/FasterXML/jackson-dataformat-xml">
032 * Jackson 2.x extension component for reading and writing XML encoded data</a>.
033 *
034 * <p>By default, this converter supports {@code application/xml}, {@code text/xml}, and
035 * {@code application/*+xml} with {@code UTF-8} character set. This can be overridden by
036 * setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
037 *
038 * <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
039 *
040 * <p>Compatible with Jackson 2.9 and higher, as of Spring 5.0.
041 *
042 * @author Sebastien Deleuze
043 * @since 4.1
044 */
045public class MappingJackson2XmlHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
046
047        /**
048         * Construct a new {@code MappingJackson2XmlHttpMessageConverter} using default configuration
049         * provided by {@code Jackson2ObjectMapperBuilder}.
050         */
051        public MappingJackson2XmlHttpMessageConverter() {
052                this(Jackson2ObjectMapperBuilder.xml().build());
053        }
054
055        /**
056         * Construct a new {@code MappingJackson2XmlHttpMessageConverter} with a custom {@link ObjectMapper}
057         * (must be a {@link XmlMapper} instance).
058         * You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
059         * @see Jackson2ObjectMapperBuilder#xml()
060         */
061        public MappingJackson2XmlHttpMessageConverter(ObjectMapper objectMapper) {
062                super(objectMapper, new MediaType("application", "xml", StandardCharsets.UTF_8),
063                                new MediaType("text", "xml", StandardCharsets.UTF_8),
064                                new MediaType("application", "*+xml", StandardCharsets.UTF_8));
065                Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required");
066        }
067
068
069        /**
070         * {@inheritDoc}
071         * The {@code ObjectMapper} parameter must be a {@link XmlMapper} instance.
072         */
073        @Override
074        public void setObjectMapper(ObjectMapper objectMapper) {
075                Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required");
076                super.setObjectMapper(objectMapper);
077        }
078
079}