下面列出了怎么用com.fasterxml.jackson.annotation.JsonManagedReference的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void setAttribute(Object pojo, AttributeMetaData attribute, Object value) {
super.setAttribute(pojo, attribute, value);
JsonManagedReference managedReference = attribute.getAnnotation(JsonManagedReference.class);
if (managedReference != null && value != null) {
Class<?> elementType = (Class<?>) attribute.getTypeArguments()[0];
PropertyDescriptor backReference = getManagedBackReference(elementType, managedReference.value());
if (backReference != null) {
Collection collection = (Collection) value;
for (Object object : collection) {
try {
PropertyUtils.setProperty(object, backReference.getName(), pojo);
} catch (Exception e) {
logger.info("Failed while setting the property {} on the class {}", backReference.getName(), value.getClass());
}
}
}
}
}
/**
* Returns a boolean indicating whether the provided field is annotated with
* some form of ignore. This method is memoized to speed up execution time
*/
boolean annotatedWithIgnore(AccessibleProperty f) {
return memoizer.annotatedWithIgnore(f, () -> {
JsonIgnore jsonIgnore = getAnnotation(f, JsonIgnore.class);
JsonIgnoreProperties classIgnoreProperties = getAnnotation(f.declaringClass, JsonIgnoreProperties.class);
JsonIgnoreProperties fieldIgnoreProperties = null;
boolean backReferenced = false;
//make sure the referring field didn't specify properties to ignore
if(referringField != null) {
fieldIgnoreProperties = getAnnotation(referringField, JsonIgnoreProperties.class);
}
//make sure the referring field didn't specify a backreference annotation
if(getAnnotation(f, JsonBackReference.class) != null && referringField != null) {
for(AccessibleProperty lastField : getAccessibleProperties(referringField.declaringClass)) {
JsonManagedReference fieldManagedReference = getAnnotation(lastField, JsonManagedReference.class);
if(fieldManagedReference != null && lastField.type.equals(f.declaringClass)) {
backReferenced = true;
break;
}
}
}
return (jsonIgnore != null && jsonIgnore.value()) ||
(classIgnoreProperties != null && asList(classIgnoreProperties.value()).contains(f.name)) ||
(fieldIgnoreProperties != null && asList(fieldIgnoreProperties.value()).contains(f.name)) ||
backReferenced;
});
}
@Override
protected void setAttribute(Object pojo, AttributeMetaData attribute, Object value) {
super.setAttribute(pojo, attribute, value);
JsonManagedReference managedReference = attribute.getAnnotation(JsonManagedReference.class);
if (managedReference != null && value != null) {
PropertyDescriptor backReference = getManagedBackReference(value.getClass(), managedReference.value());
if (backReference != null) {
try {
PropertyUtils.setProperty(value, backReference.getName(), pojo);
} catch (Exception e) {
logger.info("Failed while setting the property {} on the class {}", backReference.getName(), value.getClass());
}
}
}
}
@JsonManagedReference
public Set<TArticle> getArticles() {
return articles;
}
@JsonManagedReference("customer-data")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "userId", nullable = false)
public CustomerInfo getCustomerInfo() {
return this.customerInfo;
}
@JsonManagedReference("booked-data")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tripId", nullable = false)
public Trip getTrip() {
return this.trip;
}
@JsonManagedReference
public Set<TranslationKit> getTranslationKits() {
return translationKits;
}
@JsonManagedReference
@JsonIgnore
IMAPSSLStore getDefaultFolder();
@JsonManagedReference
@JsonIgnore
IMAPSSLStore getPersonalNamespaces();
@JsonManagedReference
@JsonIgnore
IMAPFolder getStore();
@JsonManagedReference
@JsonIgnore
@JsonBackReference
IMAPMessage getFolder();
@JsonManagedReference
@JsonIgnore
@JsonProperty("parent")
@JsonBackReference
MimeMultipart getParent();
/**
* Gets the widget containing {@link Region}s of the page
*
* @return Valid list of {@link Region}s
*/
@Override
@JsonManagedReference
public List<Region> getRegions() {
return ConvertingListProxyFactory.createProxyList(Region.class, regions);
}
@Override
@JsonManagedReference
public List<PageUser> getMembers() {
return ConvertingListProxyFactory.createProxyList(PageUser.class, members);
}
/**
* Gets the ordered list of widget instances for the region
*
* @return Valid list
*/
@Override
@JsonManagedReference
public List<RegionWidget> getRegionWidgets() {
return ConvertingListProxyFactory.createProxyList(RegionWidget.class, regionWidgets);
}
private static Optional<PropertyInfo> processProperty( RebindConfiguration configuration, TreeLogger logger, JacksonTypeOracle
typeOracle, PropertyAccessors propertyAccessors, BeanInfo beanInfo, boolean samePackage ) throws UnableToCompleteException {
boolean getterAutoDetected = isGetterAutoDetected( configuration, propertyAccessors, beanInfo );
boolean setterAutoDetected = isSetterAutoDetected( configuration, propertyAccessors, beanInfo );
boolean fieldAutoDetected = isFieldAutoDetected( configuration, propertyAccessors, beanInfo );
if ( !getterAutoDetected && !setterAutoDetected && !fieldAutoDetected && !propertyAccessors.getParameter().isPresent() ) {
// none of the field have been auto-detected, we ignore the field
return Optional.absent();
}
final String propertyName = propertyAccessors.getPropertyName();
final JType type = findType( logger, propertyAccessors, typeOracle, getterAutoDetected, setterAutoDetected, fieldAutoDetected );
PropertyInfoBuilder builder = new PropertyInfoBuilder( propertyName, type );
builder.setIgnored( isPropertyIgnored( configuration, propertyAccessors, beanInfo, type, propertyName ) );
if ( builder.isIgnored() ) {
return Optional.of( builder.build() );
}
Optional<JsonProperty> jsonProperty = propertyAccessors.getAnnotation( JsonProperty.class );
builder.setRequired( jsonProperty.isPresent() && jsonProperty.get().required() );
Optional<JsonManagedReference> jsonManagedReference = propertyAccessors.getAnnotation( JsonManagedReference.class );
builder.setManagedReference( Optional.fromNullable( jsonManagedReference.isPresent() ? jsonManagedReference.get()
.value() : null ) );
Optional<JsonBackReference> jsonBackReference = propertyAccessors.getAnnotation( JsonBackReference.class );
builder.setBackReference( Optional.fromNullable( jsonBackReference.isPresent() ? jsonBackReference.get().value() : null ) );
if ( !builder.getBackReference().isPresent() ) {
determineGetter( propertyAccessors, samePackage, getterAutoDetected, fieldAutoDetected, builder );
Optional<JsonRawValue> jsonRawValue = propertyAccessors.getAnnotation( JsonRawValue.class );
builder.setRawValue( jsonRawValue.isPresent() && jsonRawValue.get().value() );
}
determineSetter( propertyAccessors, samePackage, setterAutoDetected, fieldAutoDetected, builder );
Optional<JsonValue> jsonValue = propertyAccessors.getAnnotation( JsonValue.class );
if ( jsonValue.isPresent() && jsonValue.get().value() && builder.getGetterAccessor().isPresent() && builder.getGetterAccessor()
.get().getMethod().isPresent() ) {
builder.setValue( true );
}
Optional<JsonAnyGetter> jsonAnyGetter = propertyAccessors.getAnnotation( JsonAnyGetter.class );
if ( jsonAnyGetter.isPresent() && builder.getGetterAccessor().isPresent() && builder.getGetterAccessor().get().getMethod()
.isPresent() ) {
builder.setAnyGetter( true );
}
Optional<JsonAnySetter> jsonAnySetter = propertyAccessors.getAnnotation( JsonAnySetter.class );
if ( jsonAnySetter.isPresent() && builder.getSetterAccessor().isPresent() && builder.getSetterAccessor().get().getMethod()
.isPresent() && builder.getSetterAccessor().get().getMethod().get().getParameterTypes().length == 2 ) {
builder.setAnySetter( true );
}
Optional<JsonUnwrapped> jsonUnwrapped = propertyAccessors.getAnnotation( JsonUnwrapped.class );
if ( jsonUnwrapped.isPresent() && jsonUnwrapped.get().enabled() ) {
builder.setUnwrapped( true );
}
processBeanAnnotation( logger, typeOracle, configuration, type, propertyAccessors, builder );
builder.setFormat( propertyAccessors.getAnnotation( JsonFormat.class ) );
Optional<JsonInclude> jsonInclude = propertyAccessors.getAnnotation( JsonInclude.class );
if ( jsonInclude.isPresent() ) {
builder.setInclude( Optional.of( jsonInclude.get().value() ) );
} else {
builder.setInclude( beanInfo.getInclude() );
}
Optional<JsonIgnoreProperties> jsonIgnoreProperties = propertyAccessors.getAnnotation( JsonIgnoreProperties.class );
if ( jsonIgnoreProperties.isPresent() ) {
builder.setIgnoreUnknown( Optional.of( jsonIgnoreProperties.get().ignoreUnknown() ) );
if ( null != jsonIgnoreProperties.get().value() && jsonIgnoreProperties.get().value().length > 0 ) {
builder.setIgnoredProperties( Optional.of( jsonIgnoreProperties.get().value() ) );
}
}
return Optional.of( builder.build() );
}
@JsonManagedReference
public SimpleTreeNode2 getChild() {
return child;
}
@JsonManagedReference
public List<Item> getItems() {
return items;
}
@JsonManagedReference
public Set<AppConfigOverrideFilter> getAppConfigOverrideFilters() {
return appConfigOverrideFilters;
}