/* * Copyright 2002-2014 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 * * http://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.messaging.converter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.Charset; import java.util.concurrent.atomic.AtomicReference; import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHeaders; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.MimeType; /** * A Jackson 2 based {@link MessageConverter} implementation. * *
Tested against Jackson 2.2 and 2.3; compatible with Jackson 2.0 and higher. * * @author Rossen Stoyanchev * @author Juergen Hoeller * @since 4.0 */ public class MappingJackson2MessageConverter extends AbstractMessageConverter { // Check for Jackson 2.3's overloaded canDeserialize/canSerialize variants with cause reference private static final boolean jackson23Available = ClassUtils.hasMethod(ObjectMapper.class, "canDeserialize", JavaType.class, AtomicReference.class); private ObjectMapper objectMapper = new ObjectMapper(); private Boolean prettyPrint; public MappingJackson2MessageConverter() { super(new MimeType("application", "json", Charset.forName("UTF-8"))); } /** * Set the {@code ObjectMapper} for this converter. * If not set, a default {@link ObjectMapper#ObjectMapper() ObjectMapper} is used. *
Setting a custom-configured {@code ObjectMapper} is one way to take further * control of the JSON serialization process. For example, an extended * {@link com.fasterxml.jackson.databind.ser.SerializerFactory} can be * configured that provides custom serializers for specific types. The other * option for refining the serialization process is to use Jackson's provided * annotations on the types to be serialized, in which case a custom-configured * ObjectMapper is unnecessary. */ public void setObjectMapper(ObjectMapper objectMapper) { Assert.notNull(objectMapper, "ObjectMapper must not be null"); this.objectMapper = objectMapper; configurePrettyPrint(); } /** * Return the underlying {@code ObjectMapper} for this converter. */ public ObjectMapper getObjectMapper() { return this.objectMapper; } /** * Whether to use the {@link DefaultPrettyPrinter} when writing JSON. * This is a shortcut for setting up an {@code ObjectMapper} as follows: *
* ObjectMapper mapper = new ObjectMapper(); * mapper.configure(SerializationFeature.INDENT_OUTPUT, true); * converter.setObjectMapper(mapper); **/ public void setPrettyPrint(boolean prettyPrint) { this.prettyPrint = prettyPrint; configurePrettyPrint(); } private void configurePrettyPrint() { if (this.prettyPrint != null) { this.objectMapper.configure(SerializationFeature.INDENT_OUTPUT, this.prettyPrint); } } @Override protected boolean canConvertFrom(Message> message, Class> targetClass) { if (targetClass == null) { return false; } JavaType javaType = this.objectMapper.constructType(targetClass); if (!jackson23Available || !logger.isWarnEnabled()) { return (this.objectMapper.canDeserialize(javaType) && supportsMimeType(message.getHeaders())); } AtomicReference