Jackson2CodecSupport.java 4.3 KB
Newer Older
1
/*
J
Juergen Hoeller 已提交
2
 * Copyright 2002-2019 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
import java.util.Map;
26

27
import com.fasterxml.jackson.annotation.JsonView;
28 29 30
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
31
import org.apache.commons.logging.Log;
32

33
import org.springframework.core.GenericTypeResolver;
34
import org.springframework.core.MethodParameter;
35
import org.springframework.core.ResolvableType;
36
import org.springframework.core.codec.Hints;
R
Rossen Stoyanchev 已提交
37
import org.springframework.http.HttpLogging;
38
import org.springframework.lang.Nullable;
39
import org.springframework.util.Assert;
40
import org.springframework.util.MimeType;
41
import org.springframework.util.ObjectUtils;
42 43

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

52
	/**
53 54
	 * The key for the hint to specify a "JSON View" for encoding or decoding
	 * with the value expected to be a {@link Class}.
S
Spring Operator 已提交
55
	 * @see <a href="https://wiki.fasterxml.com/JacksonJsonViews">Jackson JSON Views</a>
56
	 */
57 58 59 60
	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: ";
61

R
Polish  
Rossen Stoyanchev 已提交
62 63 64 65
	private static final List<MimeType> DEFAULT_MIME_TYPES = Collections.unmodifiableList(
			Arrays.asList(
					new MimeType("application", "json", StandardCharsets.UTF_8),
					new MimeType("application", "*+json", StandardCharsets.UTF_8)));
66 67


R
Rossen Stoyanchev 已提交
68
	protected final Log logger = HttpLogging.forLogName(getClass());
69

70
	private final ObjectMapper objectMapper;
71

72 73
	private final List<MimeType> mimeTypes;

74 75

	/**
76
	 * Constructor with a Jackson {@link ObjectMapper} to use.
77
	 */
78
	protected Jackson2CodecSupport(ObjectMapper objectMapper, MimeType... mimeTypes) {
79 80
		Assert.notNull(objectMapper, "ObjectMapper must not be null");
		this.objectMapper = objectMapper;
R
Polish  
Rossen Stoyanchev 已提交
81 82
		this.mimeTypes = !ObjectUtils.isEmpty(mimeTypes) ?
				Collections.unmodifiableList(Arrays.asList(mimeTypes)) : DEFAULT_MIME_TYPES;
83 84
	}

85

R
Polish  
Rossen Stoyanchev 已提交
86
	public ObjectMapper getObjectMapper() {
87 88 89
		return this.objectMapper;
	}

R
Polish  
Rossen Stoyanchev 已提交
90
	/**
91
	 * Subclasses should expose this as "decodable" or "encodable" mime types.
R
Polish  
Rossen Stoyanchev 已提交
92 93 94 95 96 97
	 */
	protected List<MimeType> getMimeTypes() {
		return this.mimeTypes;
	}


98
	protected boolean supportsMimeType(@Nullable MimeType mimeType) {
99
		return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
100 101
	}

102
	protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
103 104
		TypeFactory typeFactory = this.objectMapper.getTypeFactory();
		return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
105 106
	}

107
	protected Map<String, Object> getHints(ResolvableType resolvableType) {
108 109 110 111 112 113
		MethodParameter param = getParameter(resolvableType);
		if (param != null) {
			JsonView annotation = getAnnotation(param, JsonView.class);
			if (annotation != null) {
				Class<?>[] classes = annotation.value();
				Assert.isTrue(classes.length == 1, JSON_VIEW_HINT_ERROR + param);
114
				return Hints.from(JSON_VIEW_HINT, classes[0]);
115 116
			}
		}
117
		return Hints.none();
118 119
	}

120 121
	@Nullable
	protected MethodParameter getParameter(ResolvableType type) {
J
Juergen Hoeller 已提交
122
		return (type.getSource() instanceof MethodParameter ? (MethodParameter) type.getSource() : null);
123 124
	}

125
	@Nullable
126 127
	protected abstract <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType);

128
}