List of usage examples for com.fasterxml.jackson.databind.introspect BeanPropertyDefinition getAccessor
public abstract AnnotatedMember getAccessor();
From source file:org.springframework.rest.documentation.boot.SwaggerDocumentationEndpoint.java
private void addModelForType(String type, Map<String, ClassDescriptor> responseClasses, Documentation documentation) {/*w w w . j av a 2s . c o m*/ String name = getSwaggerDataType(type); if (documentation.getModels() == null || !documentation.getModels().containsKey(name)) { DocumentationSchema schema = new DocumentationSchema(); ClassDescriptor classDescriptor = responseClasses.get(type); if (classDescriptor != null) { schema.setDescription(classDescriptor.getName()); } try { Class<?> clazz = Class.forName(type); if (clazz.isEnum()) { Object[] enumConstants = clazz.getEnumConstants(); List<String> values = new ArrayList<String>(); for (Object enumConstant : enumConstants) { values.add(enumConstant.toString()); } schema.setAllowableValues(new DocumentationAllowableListValues(values)); } else { BasicClassIntrospector introspector = new BasicClassIntrospector(); Map<String, DocumentationSchema> properties = new HashMap<String, DocumentationSchema>(); BasicBeanDescription descriptor = introspector.forSerialization( this.objectMapper.getSerializationConfig(), TypeFactory.defaultInstance().constructType(clazz), this.objectMapper.getSerializationConfig()); for (BeanPropertyDefinition property : descriptor.findProperties()) { String propertyName = property.getName(); DocumentationSchema propertySchema = new DocumentationSchema(); MethodDescriptor methodDescriptor = classDescriptor .getMethodDescriptor((Method) property.getAccessor().getAnnotated()); if (methodDescriptor != null) { Class<?> propertyClass = Class.forName(methodDescriptor.getReturnType()); propertySchema.setDescription(methodDescriptor.getSummary()); if (propertyClass.isEnum()) { Object[] enumConstants = propertyClass.getEnumConstants(); List<String> values = new ArrayList<String>(); for (Object enumConstant : enumConstants) { values.add(enumConstant.toString()); } propertySchema.setType("string"); propertySchema.setAllowableValues(new DocumentationAllowableListValues(values)); } else { propertySchema.setType(getSwaggerDataType(methodDescriptor.getReturnType())); addModelForType(methodDescriptor.getReturnType(), responseClasses, documentation); } } properties.put(propertyName, propertySchema); } schema.setProperties(properties); } } catch (ClassNotFoundException e) { e.printStackTrace(); } documentation.addModel(name, schema); } }
From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java
@Override public void serialize(Entity entity, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeStartObject();//www . ja v a 2 s. c o m try { BasicBeanDescription beanDescription = serializers.getConfig() .introspect(serializers.constructType(entity.getClass())); List<BeanPropertyDefinition> properties = beanDescription.findProperties(); for (BeanPropertyDefinition property : properties) { // 0. check if it should be serialized if (selectedProperties != null) { if (!selectedProperties.contains(property.getName())) { continue; } } // 1. is it a NavigableElement? if (NavigableElement.class.isAssignableFrom(property.getAccessor().getRawType())) { Object rawValue = property.getAccessor().getValue(entity); if (rawValue != null) { NavigableElement value = (NavigableElement) rawValue; // If navigation link set, output navigation link. if (value.getNavigationLink() != null && !value.getNavigationLink().isEmpty()) { gen.writeFieldName(property.getName() + "@iot.navigationLink"); gen.writeString(value.getNavigationLink()); } // If object should not be exported, skip any further processing. if (!value.isExportObject()) { continue; } } } // 2. check if property has CustomSerialization annotation -> use custom serializer Annotation annotation = property.getAccessor().getAnnotation(CustomSerialization.class); if (annotation != null) { serializeFieldCustomized(entity, gen, property, properties, (CustomSerialization) annotation); } else { serializeField(entity, gen, serializers, beanDescription, property); } // 3. check if property is EntitySet than eventually write count if (EntitySet.class.isAssignableFrom(property.getAccessor().getRawType())) { Object rawValue = property.getAccessor().getValue(entity); if (rawValue != null) { EntitySet set = (EntitySet) rawValue; long count = set.getCount(); if (count >= 0) { gen.writeNumberField(property.getName() + "@iot.count", count); } String nextLink = set.getNextLink(); if (nextLink != null) { gen.writeStringField(property.getName() + "@iot.nextLink", nextLink); } } } } } catch (Exception e) { LOGGER.error("could not serialize Entity", e); throw new IOException("could not serialize Entity", e); } finally { gen.writeEndObject(); } }
From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java
protected void serializeFieldCustomized(Entity entity, JsonGenerator gen, BeanPropertyDefinition property, List<BeanPropertyDefinition> properties, CustomSerialization annotation) throws Exception { // check if encoding field is present in current bean // get calue// www.j av a 2 s. c o m // call CustomSerializationManager Optional<BeanPropertyDefinition> encodingProperty = properties.stream() .filter(p -> p.getName().equals(annotation.encoding())).findFirst(); if (!encodingProperty.isPresent()) { // TODO use more specific exception type throw new Exception("can not serialize instance of class '" + entity.getClass() + "'! \n" + "Reason: trying to use custom serialization for field '" + property.getName() + "' but field '" + annotation.encoding() + "' specifying enconding is not present!"); } Object value = encodingProperty.get().getAccessor().getValue(entity); String encodingType = null; if (value != null) { encodingType = value.toString(); } String customJson = CustomSerializationManager.getInstance().getSerializer(encodingType) .serialize(property.getAccessor().getValue(entity)); if (customJson != null && !customJson.isEmpty()) { gen.writeFieldName(property.getName()); gen.writeRawValue(customJson); } }