Jackson2CodecSupport.java 4.1 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
import java.util.Map;
26

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

32
import org.springframework.core.GenericTypeResolver;
33
import org.springframework.core.MethodParameter;
34
import org.springframework.core.ResolvableType;
35
import org.springframework.lang.Nullable;
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

R
Polish  
Rossen Stoyanchev 已提交
59 60 61 62
	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)));
63 64


65
	private final ObjectMapper objectMapper;
66

67 68
	private final List<MimeType> mimeTypes;

69 70

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

80

R
Polish  
Rossen Stoyanchev 已提交
81
	public ObjectMapper getObjectMapper() {
82 83 84
		return this.objectMapper;
	}

R
Polish  
Rossen Stoyanchev 已提交
85 86 87 88 89 90 91 92
	/**
	 * Sub-classes should expose this as "decodable" or "encodable" mime types.
	 */
	protected List<MimeType> getMimeTypes() {
		return this.mimeTypes;
	}


93
	protected boolean supportsMimeType(@Nullable MimeType mimeType) {
94
		return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
95 96
	}

97
	protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
98 99
		TypeFactory typeFactory = this.objectMapper.getTypeFactory();
		return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
100 101
	}

102
	protected Map<String, Object> getHints(ResolvableType resolvableType) {
103 104 105 106 107 108 109 110 111 112
		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);
				return Collections.singletonMap(JSON_VIEW_HINT, classes[0]);
			}
		}
		return Collections.emptyMap();
113 114
	}

115 116 117
	@Nullable
	protected MethodParameter getParameter(ResolvableType type) {
		return type.getSource() instanceof MethodParameter ? (MethodParameter) type.getSource() : null;
118 119
	}

120
	@Nullable
121 122
	protected abstract <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType);

123
}