private Class<?> extractViewClass(JsonView annotation, Object conversionHint) { Class<?>[] classes = annotation.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for handler methods with exactly 1 class argument: " + conversionHint); } return classes[0]; }
private Class<?> extractViewClass(JsonView annotation, Object conversionHint) { Class<?>[] classes = annotation.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for handler methods with exactly 1 class argument: " + conversionHint); } return classes[0]; }
@Override protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) { JsonView ann = returnType.getMethodAnnotation(JsonView.class); Assert.state(ann != null, "No JsonView annotation"); Class<?>[] classes = ann.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for response body advice with exactly 1 class argument: " + returnType); } bodyContainer.setSerializationView(classes[0]); }
@Override public Class<?>[] findViews(Annotated a) { JsonView ann = _findAnnotation(a, JsonView.class); return (ann == null) ? null : ann.value(); }
private boolean hiddenByJsonView(Annotation[] annotations, AnnotatedType type) { JsonView jsonView = type.getJsonViewAnnotation(); if (jsonView == null) return false; Class<?>[] filters = jsonView.value(); boolean containsJsonViewAnnotation = false; for (Annotation ant : annotations) { if (ant instanceof JsonView) { containsJsonViewAnnotation = true; Class<?>[] views = ((JsonView) ant).value(); for (Class<?> f : filters) { for (Class<?> v : views) { if (v == f || v.isAssignableFrom(f)) { return false; } } } } } return containsJsonViewAnnotation; }
@Override public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException { JsonView ann = methodParameter.getParameterAnnotation(JsonView.class); Assert.state(ann != null, "No JsonView annotation"); Class<?>[] classes = ann.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter); } return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]); }
/** * Decorate the name based on the JsonView */ protected String decorateModelName(AnnotatedType type, String originalName) { if (StringUtils.isBlank(originalName)) { return originalName; } String name = originalName; if (type.getJsonViewAnnotation() != null && type.getJsonViewAnnotation().value().length > 0) { String COMBINER = "-or-"; StringBuilder sb = new StringBuilder(); for (Class<?> view : type.getJsonViewAnnotation().value()) { sb.append(view.getSimpleName()).append(COMBINER); } String suffix = sb.substring(0, sb.length() - COMBINER.length()); name = originalName + "_" + suffix; } return name; }
@Override protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) { JsonView ann = returnType.getMethodAnnotation(JsonView.class); Assert.state(ann != null, "No JsonView annotation"); Class<?>[] classes = ann.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for response body advice with exactly 1 class argument: " + returnType); } bodyContainer.setSerializationView(classes[0]); }
protected Map<String, Object> getHints(ResolvableType resolvableType) { 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 Hints.from(JSON_VIEW_HINT, classes[0]); } } return Hints.none(); }
@Override public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException { JsonView ann = methodParameter.getParameterAnnotation(JsonView.class); Assert.state(ann != null, "No JsonView annotation"); Class<?>[] classes = ann.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter); } return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]); }
protected Map<String, Object> getHints(ResolvableType resolvableType) { 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 Hints.from(JSON_VIEW_HINT, classes[0]); } } return Hints.none(); }
private Class<?> extractViewClass(JsonView annotation, Object conversionHint) { Class<?>[] classes = annotation.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for handler methods with exactly 1 class argument: " + conversionHint); } return classes[0]; }
public class JsonViewInterceptor extends HandlerInterceptorAdapter { @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { HandlerMethod handlerMethod = (HandlerMethod) handler; JsonView jsonViewAnnotation = handlerMethod.getMethodAnnotation(JsonView.class); if(jsonViewAnnotation != null) modelAndView.addObject("json.JsonView", jsonViewAnnotation.value()); } }
protected void addAnnotation(Class<? extends Annotation> type, Annotation annotation, boolean forWriting) { if (type == JsonView.class) { // Can only use one view; but if multiple defined, use first (no exception) Class<?>[] views = ((JsonView) annotation).value(); _activeView = (views.length > 0) ? views[0] : null; } else if (type == JacksonFeatures.class) { JacksonFeatures feats = (JacksonFeatures) annotation; if (forWriting) { _serEnable = nullIfEmpty(feats.serializationEnable()); _serDisable = nullIfEmpty(feats.serializationDisable()); } else { _deserEnable = nullIfEmpty(feats.deserializationEnable()); _deserDisable = nullIfEmpty(feats.deserializationDisable()); } } else if (type == JsonRootName.class) { _rootName = ((JsonRootName) annotation).value(); } else if (type == JacksonAnnotationsInside.class) { // skip; processed below (in parent), so encountering here is of no use } else { // For all unrecognized types, check meta-annotation(s) to see if they are bundles JacksonAnnotationsInside inside = type.getAnnotation(JacksonAnnotationsInside.class); if (inside != null) { add(type.getAnnotations(), forWriting); } } }
private Class<?> extractViewClass(JsonView annotation, Object conversionHint) { Class<?>[] classes = annotation.value(); if (classes.length != 1) { throw new IllegalArgumentException( "@JsonView only supported for handler methods with exactly 1 class argument: " + conversionHint); } return classes[0]; }
@Override public Class<?>[] findViews(Annotated a) { JsonView ann = _findAnnotation(a, JsonView.class); return (ann == null) ? null : ann.value(); }
@Override public Class<?>[] findViews(Annotated a) { JsonView ann = a.getAnnotation(JsonView.class); return (ann == null) ? null : ann.value(); }
@Override public boolean test(Field field, CallContext context) { checkNotNull(field); checkNotNull(context); final JsonView jsonView = field.getAnnotation(JsonView.class); if (jsonView != null) { @SuppressWarnings("rawtypes") Optional<Set<Class>> viewClasses = context.getAdditionalObjectsFor(Class.class); return !viewClasses.isPresent() || Arrays.stream(jsonView.value()).anyMatch(c->viewClasses.get().contains(c)); } return true; }
@Override public Class<?>[] findViews(Annotated a) { JsonView ann = _findAnnotation(a, JsonView.class); return (ann == null) ? null : ann.value(); }
@Override public Class<?>[] findViews(Annotated a) { JsonView ann = _findAnnotation(a, JsonView.class); return (ann == null) ? null : ann.value(); }