Jackson2CodecSupport.java 3.8 KB
Newer Older
1
/*
2
 * Copyright 2002-2017 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.http.codec.json;

19
import java.lang.annotation.Annotation;
20 21 22
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
23
import java.util.Collections;
24
import java.util.List;
25 26
import java.util.Map;
import java.util.Optional;
27

28
import com.fasterxml.jackson.annotation.JsonView;
29 30 31 32
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

33
import org.springframework.core.GenericTypeResolver;
34
import org.springframework.core.MethodParameter;
35
import org.springframework.core.ResolvableType;
36
import org.springframework.util.Assert;
37
import org.springframework.util.MimeType;
38
import org.springframework.util.ObjectUtils;
39 40

/**
41
 * Base class providing support methods for Jackson 2.9 encoding and decoding.
42
 *
43
 * @author Sebastien Deleuze
44 45
 * @author Rossen Stoyanchev
 * @since 5.0
46
 */
47
public abstract class Jackson2CodecSupport {
48

49
	/**
50 51
	 * The key for the hint to specify a "JSON View" for encoding or decoding
	 * with the value expected to be a {@link Class}.
52 53
	 * @see <a href="http://wiki.fasterxml.com/JacksonJsonViews">Jackson JSON Views</a>
	 */
54 55 56 57
	public static final String JSON_VIEW_HINT = Jackson2CodecSupport.class.getName() + ".jsonView";

	private static final String JSON_VIEW_HINT_ERROR =
			"@JsonView only supported for write hints with exactly 1 class argument: ";
58

59 60 61 62 63
	protected static final List<MimeType> JSON_MIME_TYPES = Arrays.asList(
				new MimeType("application", "json", StandardCharsets.UTF_8),
				new MimeType("application", "*+json", StandardCharsets.UTF_8));


64
	protected final ObjectMapper objectMapper;
65

66 67
	private final List<MimeType> mimeTypes;

68 69

	/**
70
	 * Constructor with a Jackson {@link ObjectMapper} to use.
71
	 */
72
	protected Jackson2CodecSupport(ObjectMapper objectMapper, MimeType... mimeTypes) {
73 74
		Assert.notNull(objectMapper, "ObjectMapper must not be null");
		this.objectMapper = objectMapper;
75
		this.mimeTypes = !ObjectUtils.isEmpty(mimeTypes) ? Arrays.asList(mimeTypes) : JSON_MIME_TYPES;
76 77
	}

78

79
	protected boolean supportsMimeType(MimeType mimeType) {
80
		return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
81 82
	}

83
	protected JavaType getJavaType(Type type, Class<?> contextClass) {
84 85
		TypeFactory typeFactory = this.objectMapper.getTypeFactory();
		return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
86 87
	}

88 89 90 91 92 93 94 95 96 97 98 99
	protected Map<String, Object> getHints(ResolvableType actualType) {
		return getParameter(actualType)
				.flatMap(parameter -> Optional.ofNullable(getAnnotation(parameter, JsonView.class))
						.map(annotation -> {
							Class<?>[] classes = annotation.value();
							Assert.isTrue(classes.length == 1, JSON_VIEW_HINT_ERROR + parameter);
							return Collections.<String, Object>singletonMap(JSON_VIEW_HINT, classes[0]);
						}))
				.orElse(Collections.emptyMap());
	}

	protected Optional<MethodParameter> getParameter(ResolvableType type) {
100
		return Optional.ofNullable(type.getSource() instanceof MethodParameter ?
101 102 103 104 105
				(MethodParameter) type.getSource() : null);
	}

	protected abstract <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType);

106
}