001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.http.converter.json; 018 019import com.fasterxml.jackson.databind.ser.FilterProvider; 020 021import org.springframework.lang.Nullable; 022 023/** 024 * A simple holder for the POJO to serialize via 025 * {@link MappingJackson2HttpMessageConverter} along with further 026 * serialization instructions to be passed in to the converter. 027 * 028 * <p>On the server side this wrapper is added with a 029 * {@code ResponseBodyInterceptor} after content negotiation selects the 030 * converter to use but before the write. 031 * 032 * <p>On the client side, simply wrap the POJO and pass it in to the 033 * {@code RestTemplate}. 034 * 035 * @author Rossen Stoyanchev 036 * @since 4.1 037 */ 038public class MappingJacksonValue { 039 040 private Object value; 041 042 @Nullable 043 private Class<?> serializationView; 044 045 @Nullable 046 private FilterProvider filters; 047 048 049 /** 050 * Create a new instance wrapping the given POJO to be serialized. 051 * @param value the Object to be serialized 052 */ 053 public MappingJacksonValue(Object value) { 054 this.value = value; 055 } 056 057 058 /** 059 * Modify the POJO to serialize. 060 */ 061 public void setValue(Object value) { 062 this.value = value; 063 } 064 065 /** 066 * Return the POJO that needs to be serialized. 067 */ 068 public Object getValue() { 069 return this.value; 070 } 071 072 /** 073 * Set the serialization view to serialize the POJO with. 074 * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class) 075 * @see com.fasterxml.jackson.annotation.JsonView 076 */ 077 public void setSerializationView(@Nullable Class<?> serializationView) { 078 this.serializationView = serializationView; 079 } 080 081 /** 082 * Return the serialization view to use. 083 * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class) 084 * @see com.fasterxml.jackson.annotation.JsonView 085 */ 086 @Nullable 087 public Class<?> getSerializationView() { 088 return this.serializationView; 089 } 090 091 /** 092 * Set the Jackson filter provider to serialize the POJO with. 093 * @since 4.2 094 * @see com.fasterxml.jackson.databind.ObjectMapper#writer(FilterProvider) 095 * @see com.fasterxml.jackson.annotation.JsonFilter 096 * @see Jackson2ObjectMapperBuilder#filters(FilterProvider) 097 */ 098 public void setFilters(@Nullable FilterProvider filters) { 099 this.filters = filters; 100 } 101 102 /** 103 * Return the Jackson filter provider to use. 104 * @since 4.2 105 * @see com.fasterxml.jackson.databind.ObjectMapper#writer(FilterProvider) 106 * @see com.fasterxml.jackson.annotation.JsonFilter 107 */ 108 @Nullable 109 public FilterProvider getFilters() { 110 return this.filters; 111 } 112 113}