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 021/** 022 * A simple holder for the POJO to serialize via 023 * {@link MappingJackson2HttpMessageConverter} along with further 024 * serialization instructions to be passed in to the converter. 025 * 026 * <p>On the server side this wrapper is added with a 027 * {@code ResponseBodyInterceptor} after content negotiation selects the 028 * converter to use but before the write. 029 * 030 * <p>On the client side, simply wrap the POJO and pass it in to the 031 * {@code RestTemplate}. 032 * 033 * @author Rossen Stoyanchev 034 * @since 4.1 035 */ 036public class MappingJacksonValue { 037 038 private Object value; 039 040 private Class<?> serializationView; 041 042 private FilterProvider filters; 043 044 private String jsonpFunction; 045 046 047 /** 048 * Create a new instance wrapping the given POJO to be serialized. 049 * @param value the Object to be serialized 050 */ 051 public MappingJacksonValue(Object value) { 052 this.value = value; 053 } 054 055 056 /** 057 * Modify the POJO to serialize. 058 */ 059 public void setValue(Object value) { 060 this.value = value; 061 } 062 063 /** 064 * Return the POJO that needs to be serialized. 065 */ 066 public Object getValue() { 067 return this.value; 068 } 069 070 /** 071 * Set the serialization view to serialize the POJO with. 072 * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class) 073 * @see com.fasterxml.jackson.annotation.JsonView 074 */ 075 public void setSerializationView(Class<?> serializationView) { 076 this.serializationView = serializationView; 077 } 078 079 /** 080 * Return the serialization view to use. 081 * @see com.fasterxml.jackson.databind.ObjectMapper#writerWithView(Class) 082 * @see com.fasterxml.jackson.annotation.JsonView 083 */ 084 public Class<?> getSerializationView() { 085 return this.serializationView; 086 } 087 088 /** 089 * Set the Jackson filter provider to serialize the POJO with. 090 * @since 4.2 091 * @see com.fasterxml.jackson.databind.ObjectMapper#writer(FilterProvider) 092 * @see com.fasterxml.jackson.annotation.JsonFilter 093 * @see Jackson2ObjectMapperBuilder#filters(FilterProvider) 094 */ 095 public void setFilters(FilterProvider filters) { 096 this.filters = filters; 097 } 098 099 /** 100 * Return the Jackson filter provider to use. 101 * @since 4.2 102 * @see com.fasterxml.jackson.databind.ObjectMapper#writer(FilterProvider) 103 * @see com.fasterxml.jackson.annotation.JsonFilter 104 */ 105 public FilterProvider getFilters() { 106 return this.filters; 107 } 108 109 /** 110 * Set the name of the JSONP function name. 111 * @deprecated Will be removed as of Spring Framework 5.1, use 112 * <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead. 113 */ 114 @Deprecated 115 public void setJsonpFunction(String functionName) { 116 this.jsonpFunction = functionName; 117 } 118 119 /** 120 * Return the configured JSONP function name. 121 * @deprecated Will be removed as of Spring Framework 5.1, use 122 * <a href="https://docs.spring.io/spring/docs/4.3.x/spring-framework-reference/html/cors.html">CORS</a> instead. 123 */ 124 @Deprecated 125 public String getJsonpFunction() { 126 return this.jsonpFunction; 127 } 128 129}