Jackson2CodecSupport.java 3.9 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.lang.Nullable;
37
import org.springframework.util.Assert;
38
import org.springframework.util.MimeType;
39
import org.springframework.util.ObjectUtils;
40 41

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

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

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


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;
76
		this.mimeTypes = !ObjectUtils.isEmpty(mimeTypes) ? Arrays.asList(mimeTypes) : JSON_MIME_TYPES;
77 78
	}

79

80 81 82 83
	protected ObjectMapper objectMapper() {
		return this.objectMapper;
	}

84
	protected boolean supportsMimeType(@Nullable MimeType mimeType) {
85
		return (mimeType == null || this.mimeTypes.stream().anyMatch(m -> m.isCompatibleWith(mimeType)));
86 87
	}

88
	protected JavaType getJavaType(Type type, @Nullable Class<?> contextClass) {
89 90
		TypeFactory typeFactory = this.objectMapper.getTypeFactory();
		return typeFactory.constructType(GenericTypeResolver.resolveType(type, contextClass));
91 92
	}

93 94
	protected Map<String, Object> getHints(ResolvableType resolvableType) {
		return getParameter(resolvableType)
95 96 97 98 99 100 101 102 103 104
				.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) {
105
		return Optional.ofNullable(type.getSource() instanceof MethodParameter ?
106 107 108
				(MethodParameter) type.getSource() : null);
	}

109
	@Nullable
110 111
	protected abstract <A extends Annotation> A getAnnotation(MethodParameter parameter, Class<A> annotType);

112
}