下面列出了com.fasterxml.jackson.databind.ObjectMapper#addMixInAnnotations ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Test the Mixin to make sure that it capitalizes the values, and is
* different from an ObjectMapper without the Mixin.
*/
@Test
public void testReturnsCapitalSWithMixin() throws JsonProcessingException {
String capitalS = "S";
String lowercaseS = "s";
ObjectMapper mapperWith = new ObjectMapper();
mapperWith.setSerializationInclusion(Include.NON_NULL);
mapperWith.addMixInAnnotations(AttributeValue.class,
AttributeValueMixIn.class);
String withMixIn = mapperWith.writeValueAsString(sampleScanResult()
.get(0));
ObjectMapper mapperWithout = new ObjectMapper();
mapperWithout.setSerializationInclusion(Include.NON_NULL);
String withoutMixIn = mapperWithout
.writeValueAsString(sampleScanResult().get(0));
assertTrue(withMixIn.contains(capitalS));
assertTrue(withoutMixIn.contains(lowercaseS));
}
@SuppressWarnings("deprecation")
@RequestMapping(value = "/api/path", method = RequestMethod.GET)
@ResponseBody
public String getPathsForProfile(Model model, String profileIdentifier,
@RequestParam(value = "typeFilter[]", required = false) String[] typeFilter,
@RequestParam(value = "clientUUID", defaultValue = Constants.PROFILE_CLIENT_DEFAULT_ID) String clientUUID) throws Exception {
int profileId = ControllerUtils.convertProfileIdentifier(profileIdentifier);
List<EndpointOverride> paths = PathOverrideService.getInstance().getPaths(profileId, clientUUID, typeFilter);
HashMap<String, Object> jqReturn = Utils.getJQGridJSON(paths, "paths");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixInAnnotations(Object.class, ViewFilters.GetPathFilter.class);
String[] ignorableFieldNames = {"possibleEndpoints", "enabledEndpoints"};
FilterProvider filters = new SimpleFilterProvider().addFilter("Filter properties from the PathController GET",
SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
ObjectWriter writer = objectMapper.writer(filters);
return writer.writeValueAsString(jqReturn);
}
@RequestMapping(value = "/api/path/test", method = RequestMethod.GET)
@ResponseBody
public String testPath(@RequestParam String url, @RequestParam String profileIdentifier,
@RequestParam Integer requestType) throws Exception {
int profileId = ControllerUtils.convertProfileIdentifier(profileIdentifier);
List<EndpointOverride> trySelectedRequestPaths = PathOverrideService.getInstance().getSelectedPaths(Constants.OVERRIDE_TYPE_REQUEST,
ClientService.getInstance().findClient("-1", profileId),
ProfileService.getInstance().findProfile(profileId), url, requestType, true);
HashMap<String, Object> jqReturn = Utils.getJQGridJSON(trySelectedRequestPaths, "paths");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixInAnnotations(Object.class, ViewFilters.GetPathFilter.class);
String[] ignorableFieldNames = {"possibleEndpoints", "enabledEndpoints"};
FilterProvider filters = new SimpleFilterProvider().addFilter("Filter properties from the PathController GET",
SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames));
ObjectWriter writer = objectMapper.writer(filters);
return writer.writeValueAsString(jqReturn);
}
@PostConstruct
public void postConstruct() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// abstract info classes
objectMapper.addMixInAnnotations(AbstractAttributeInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(ClientLayerInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(ClientUserDataInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(ClientWidgetInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(ConstraintInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(LayerExtraInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(PrintComponentInfo.class, TypeInfoMixin.class);
// abstract SLD classes
objectMapper.addMixInAnnotations(ExpressionInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(ExpressionTypeInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(ComparisonOpsTypeInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(LogicOpsTypeInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(SpatialOpsTypeInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(AbstractGeometryCollectionInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(AbstractGeometryInfo.class, TypeInfoMixin.class);
objectMapper.addMixInAnnotations(SymbolizerTypeInfo.class, TypeInfoMixin.class);
}
@Override
protected ObjectMapper createObjectMapper() {
ObjectMapper mapper = super.createObjectMapper();
mapper.addMixInAnnotations(LocatorIF.class, MLocator.class);
mapper.addMixInAnnotations(TopicIF.class, MTopic.class);
mapper.addMixInAnnotations(TopicNameIF.class, MTopicName.class);
mapper.addMixInAnnotations(VariantNameIF.class, MVariantName.class);
mapper.addMixInAnnotations(OccurrenceIF.class, MOccurrence.class);
mapper.addMixInAnnotations(AssociationIF.class, MAssociation.class);
mapper.addMixInAnnotations(AssociationRoleIF.class, MAssociationRole.class);
mapper.addMixInAnnotations(TopicMapIF.class, MTopicMapAsValue.class);
mapper.addMixInAnnotations(TopicMapReferenceIF.class, MTopicMapReference.class);
mapper.addMixInAnnotations(TopicMapSourceIF.class, MTopicMapSource.class);
@SuppressWarnings("unchecked")
Map<Class<?>, Class<?>> additional = (Map<Class<?>, Class<?>>) Response.getCurrent().getAttributes().get(ADDITIONAL_MIXINS_ATTRIBUTE);
if ((additional != null) && (!additional.isEmpty())) {
for (Map.Entry<Class<?>, Class<?>> entry : additional.entrySet()) {
mapper.addMixInAnnotations(entry.getKey(), entry.getValue());
}
}
for (JsonParser.Feature feature : JsonParser.Feature.values()) {
Parameter parameter = ContextUtils.getParameter(ContextUtils.getCurrentApplicationContext(), JSONPARSER_FEATURE + feature.name());
if (parameter != null) {
mapper.configure(feature, ContextUtils.getParameterAsBoolean(parameter, feature.enabledByDefault() || DEFAULT_PARSER_FEATURES.contains(feature)));
}
}
return mapper;
}
/**
* override spring-boot default ObjectMapper to configure output(serialization) json.
*
* @see org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration.ObjectMappers#jacksonObjectMapper()
*/
@Bean
public ObjectMapper jacksonObjectMapper() {
// use different visibility for serialization(output json)
// I want to ONLY change the visibility for serialization, but couldn't find nice way to do it.
// maybe related to this issue: https://github.com/FasterXML/jackson-databind/issues/352
// for now, override ObjectMapper and set new SerializationConfig in instance initializer.
// TODO: find correct way to do this.
final ObjectMapper mapper = new ObjectMapper() {
{
// use instance fields for output json
_serializationConfig = _serializationConfig.with(
_serializationConfig.getDefaultVisibilityChecker()
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
);
}
};
// mix-in to ignore thrift specific fields for serialization.
mapper.addMixInAnnotations(Object.class, ThriftPropertyJacksonFilter.class);
return mapper;
}
/**
* Create a JSON serializer for these validation results.
* @param results
*/
public JsonSerializer (FeedValidationResultSet results) {
super(results);
mapper = new ObjectMapper();
mapper.addMixInAnnotations(Rectangle2D.class, Rectangle2DMixIn.class);
SimpleModule deser = new SimpleModule();
deser.addDeserializer(Rectangle2D.class, new Rectangle2DDeserializer());
mapper.registerModule(deser);
SimpleFilterProvider filters = new SimpleFilterProvider();
filters.addFilter("bbox", SimpleBeanPropertyFilter.filterOutAllExcept("west", "east", "south", "north"));
writer = mapper.writer(filters);
}
public GMailMessageActivitySerializer(GMailProvider provider) {
this.provider = provider;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, Boolean.FALSE);
mapper.addMixInAnnotations(IMAPSSLStore.class, MessageMixIn.class);
mapper.addMixInAnnotations(IMAPFolder.class, MessageMixIn.class);
mapper.addMixInAnnotations(IMAPMessage.class, MessageMixIn.class);
mapper.addMixInAnnotations(MimeMultipart.class, MessageMixIn.class);
mapper.addMixInAnnotations(JavaMailGmailMessage.class, MessageMixIn.class);
}