001/*
002 * Copyright 2002-2017 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 com.fasterxml.jackson.databind.ObjectMapper;
020import com.fasterxml.jackson.dataformat.xml.XmlMapper;
021
022import org.springframework.http.MediaType;
023import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
024import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
025import org.springframework.util.Assert;
026
027/**
028 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
029 * that can read and write XML using <a href="https://github.com/FasterXML/jackson-dataformat-xml">
030 * Jackson 2.x extension component for reading and writing XML encoded data</a>.
031 *
032 * <p>By default, this converter supports {@code application/xml}, {@code text/xml}, and
033 * {@code application/*+xml} with {@code UTF-8} character set. This can be overridden by
034 * setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
035 *
036 * <p>The default constructor uses the default configuration provided by {@link Jackson2ObjectMapperBuilder}.
037 *
038 * <p>Compatible with Jackson 2.6 and higher, as of Spring 4.3.
039 *
040 * @author Sebastien Deleuze
041 * @since 4.1
042 */
043public class MappingJackson2XmlHttpMessageConverter extends AbstractJackson2HttpMessageConverter {
044
045        /**
046         * Construct a new {@code MappingJackson2XmlHttpMessageConverter} using default configuration
047         * provided by {@code Jackson2ObjectMapperBuilder}.
048         */
049        public MappingJackson2XmlHttpMessageConverter() {
050                this(Jackson2ObjectMapperBuilder.xml().build());
051        }
052
053        /**
054         * Construct a new {@code MappingJackson2XmlHttpMessageConverter} with a custom {@link ObjectMapper}
055         * (must be a {@link XmlMapper} instance).
056         * You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
057         * @see Jackson2ObjectMapperBuilder#xml()
058         */
059        public MappingJackson2XmlHttpMessageConverter(ObjectMapper objectMapper) {
060                super(objectMapper, new MediaType("application", "xml"),
061                                new MediaType("text", "xml"),
062                                new MediaType("application", "*+xml"));
063                Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required");
064        }
065
066
067        /**
068         * {@inheritDoc}
069         * The {@code ObjectMapper} parameter must be a {@link XmlMapper} instance.
070         */
071        @Override
072        public void setObjectMapper(ObjectMapper objectMapper) {
073                Assert.isInstanceOf(XmlMapper.class, objectMapper, "XmlMapper required");
074                super.setObjectMapper(objectMapper);
075        }
076
077}