如何在Spring 中設定ObjectMapper
問題:
問題:問題:
你想在Spring 中設定ObjectMapper Spring僅序列化用@JsonProperty註解的元素。但是,儘管遵循建議的說明,NumbersOfNewEvents 類別在序列化時仍然包含所有屬性。
說明:public class CompanyObjectMapper extends ObjectMapper { public CompanyObjectMapper() { super(); SerializationConfig config = getSerializationConfig(); config.withView(Some.class) // Specify which view to use .withVisibility(JsonAutoDetect.Visibility.NONE) .withVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); } }
解決方案:
要達到期望的結果,可以使用更有針對性的方法來配置可見性檢查員。以下是一個範例:@Configuration public class JacksonConfiguration { @Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); // Enable default typing for polymorphic types mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); // Allow serialization of fields mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true); // Enable default view inclusion return mapper; } }
以上是如何配置Spring的ObjectMapper只序列化@JsonProperty註解的元素?的詳細內容。更多資訊請關注PHP中文網其他相關文章!