com.fasterxml.jackson.annotation.JsonBackReference Java Examples

The following examples show how to use com.fasterxml.jackson.annotation.JsonBackReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonModule.java    From jsonschema-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Determine whether a given field should be ignored, according to various jackson annotations for that purpose,
 * <br>
 * e.g. {@code JsonBackReference}, {@code JsonIgnore}, {@code JsonIgnoreType}, {@code JsonIgnoreProperties}
 *
 * @param field field to check
 * @return whether field should be excluded
 */
protected boolean shouldIgnoreField(FieldScope field) {
    if (field.getAnnotationConsideringFieldAndGetter(JsonBackReference.class) != null) {
        return true;
    }
    // instead of re-creating the various ways a property may be included/excluded in jackson: just use its built-in introspection
    BeanDescription beanDescription = this.getBeanDescriptionForClass(field.getDeclaringType());
    // some kinds of field ignorals are only available via an annotation introspector
    Set<String> ignoredProperties = this.objectMapper.getSerializationConfig().getAnnotationIntrospector()
            .findPropertyIgnorals(beanDescription.getClassInfo()).getIgnored();
    String fieldName = field.getName();
    if (ignoredProperties.contains(fieldName)) {
        return true;
    }
    // other kinds of field ignorals are handled implicitly, i.e. are only available by way of being absent
    return beanDescription.findProperties().stream()
            .noneMatch(propertyDefinition -> fieldName.equals(propertyDefinition.getInternalName()));
}
 
Example #2
Source File: BiDirectionalObjectResolver.java    From minnal with Apache License 2.0 5 votes vote down vote up
private PropertyDescriptor getManagedBackReference(Class<?> clazz, String name) {
    List<PropertyDescriptor> descriptors = PropertyUtil.getPopertiesWithAnnotation(clazz, JsonBackReference.class);
    if (descriptors != null) {
        for (PropertyDescriptor descriptor : descriptors) {
            JsonBackReference backReference = PropertyUtil.getAnnotation(descriptor, JsonBackReference.class);
            if (backReference.value().equals(name)) {
                return descriptor;
            }
        }
    }
    return null;
}
 
Example #3
Source File: BiDirectionalCollectionResolver.java    From minnal with Apache License 2.0 5 votes vote down vote up
private PropertyDescriptor getManagedBackReference(Class<?> clazz, String name) {
    List<PropertyDescriptor> descriptors = PropertyUtil.getPopertiesWithAnnotation(clazz, JsonBackReference.class);
    if (descriptors != null) {
        for (PropertyDescriptor descriptor : descriptors) {
            JsonBackReference backReference = PropertyUtil.getAnnotation(descriptor, JsonBackReference.class);
            if (backReference.value().equals(name)) {
                return descriptor;
            }
        }
    }
    return null;
}
 
Example #4
Source File: JsonViewSerializer.java    From json-view with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
  });
}
 
Example #5
Source File: ExcludeAnnotationsConvertorTest.java    From minnal with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void setup() {
	convertor = spy(new ExcludeAnnotationsConvertor(Lists.<Class<? extends Annotation>>newArrayList(JsonBackReference.class)));
	model = mock(Model.class);
	properties = mock(LinkedHashMap.class);
	when(model.properties()).thenReturn(properties);
}
 
Example #6
Source File: ExcludeAnnotationsConvertorTest.java    From minnal with Apache License 2.0 4 votes vote down vote up
/**
 * @return the field2
 */
@JsonBackReference
public String getField2() {
	return field2;
}
 
Example #7
Source File: JpaRegion.java    From attic-rave with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the associated page
 *
 * @return the associated page
 */
@Override
@JsonBackReference
public Page getPage() {
    return page;
}
 
Example #8
Source File: RegionWidget.java    From attic-rave with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
Region getRegion();
 
Example #9
Source File: ApiBundle.java    From minnal with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the model convertor
 * 
 * @return
 */
protected ExcludeAnnotationsConvertor getExcludeAnnotationsConvertor() {
	List<Class<? extends Annotation>> excludedAnnotations = Lists.<Class<? extends Annotation>>newArrayList(JsonBackReference.class);
	excludedAnnotations.addAll(configuration.getExcludedAnnotations());
	return new ExcludeAnnotationsConvertor(excludedAnnotations);
}
 
Example #10
Source File: ApiBundleTest.java    From minnal with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldGetExcludeAnnotationsConverter() {
	apiBundle.init(container, bundleConfiguration);
	ExcludeAnnotationsConvertor convertor = apiBundle.getExcludeAnnotationsConvertor();
	assertEquals(convertor.getExcludeAnnotations(), Lists.newArrayList(JsonBackReference.class, JsonIgnore.class));
}
 
Example #11
Source File: JpaRegionWidget.java    From attic-rave with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the associated region
 *
 * @return the region
 */
@Override
@JsonBackReference
public Region getRegion() {
    return region;
}
 
Example #12
Source File: PropertyProcessor.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
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() );
}
 
Example #13
Source File: JsonManagedAndBackReferenceTester.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public SimpleTreeNode2 getParent() {
    return parent;
}
 
Example #14
Source File: JsonManagedAndBackReferenceTester.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public Parent getParent() {
    return parent;
}
 
Example #15
Source File: TaskAssignment.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "documentID", nullable = false, insertable = false, updatable = false)
@JsonBackReference
public Document getDocument() {
	return this.document;
}
 
Example #16
Source File: DocumentNominalLabel.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "nominalLabelID", nullable = false, insertable = false, updatable = false)
@JsonBackReference
public NominalLabel getNominalLabel() {
	return this.nominalLabel;
}
 
Example #17
Source File: DocumentNominalLabel.java    From AIDR with GNU Affero General Public License v3.0 4 votes vote down vote up
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "documentID", nullable = false, insertable = false, updatable = false)
@JsonBackReference
public Document getDocument() {
	return this.document;
}
 
Example #18
Source File: AppConfigOverrideFilter.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public ApigeeApp getApplication() {
	return application;
}
 
Example #19
Source File: AppConfigURLRegex.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public ApigeeMonitoringSettings getAppConfig() {
	return appConfig;
}
 
Example #20
Source File: AppConfigCustomParameter.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public ApigeeMonitoringSettings getAppConfig() {
	return appConfig;
}
 
Example #21
Source File: ActUtils.java    From Shop-for-JavaWeb with MIT License 4 votes vote down vote up
@SuppressWarnings({ "unused" })
public static Map<String, Object> getMobileEntity(Object entity,String spiltType){
	if(spiltType==null){
		spiltType="@";
	}
	Map<String, Object> map = Maps.newHashMap();

	List<String> field = Lists.newArrayList();
	List<String> value = Lists.newArrayList();
	List<String> chinesName =Lists.newArrayList();
	
	try{
		for (Method m : entity.getClass().getMethods()){
			if (m.getAnnotation(JsonIgnore.class) == null && m.getAnnotation(JsonBackReference.class) == null && m.getName().startsWith("get")){
				if (m.isAnnotationPresent(FieldName.class)) {
					Annotation p = m.getAnnotation(FieldName.class);
					FieldName fieldName=(FieldName) p;
					chinesName.add(fieldName.value());
				}else{
					chinesName.add("");
				}
				if (m.getName().equals("getAct")){
					Object act = m.invoke(entity, new Object[]{});
					Method actMet = act.getClass().getMethod("getTaskId");
					map.put("taskId", ObjectUtils.toString(m.invoke(act, new Object[]{}), ""));
				}else{
					field.add(StringUtils.uncapitalize(m.getName().substring(3)));
					value.add(ObjectUtils.toString(m.invoke(entity, new Object[]{}), ""));
				}
			}
		}
	}catch (Exception e) {
		e.printStackTrace();
	}
	
	map.put("beanTitles", StringUtils.join(field, spiltType));
	map.put("beanInfos", StringUtils.join(value, spiltType));
	map.put("chineseNames", StringUtils.join(chinesName, spiltType));
	
	return map;
}
 
Example #22
Source File: Lane.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public Process getParentProcess() {
  return parentProcess;
}
 
Example #23
Source File: TArticle.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
@JsonBackReference
public TUser getUser() {
    return user;
}
 
Example #24
Source File: PaymentAp.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@XmlTransient
   @JsonBackReference("paymentAp")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "paymentAp")
public Set<Invoice> getInvoices() {
	return this.invoices;
}
 
Example #25
Source File: PaymentAp.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@XmlTransient
   @JsonBackReference("paymentAp")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "paymentAp")
public Set<Invoice> getInvoices() {
	return this.invoices;
}
 
Example #26
Source File: StationDetail.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@JsonBackReference("trips-data")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stationDetail")
public Set<Trip> getTrips() {
	return this.trips;
}
 
Example #27
Source File: CustomerInfo.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@JsonBackReference("customer-data")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customerInfo")
public Set<BookedTrip> getBookedTrips() {
	return this.bookedTrips;
}
 
Example #28
Source File: Trip.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@JsonBackReference("trips-data")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "stationId", nullable = false)
public StationDetail getStationDetail() {
	return this.stationDetail;
}
 
Example #29
Source File: Trip.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@JsonBackReference("booked-data")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "trip")
public Set<BookedTrip> getBookedTrips() {
	return this.bookedTrips;
}
 
Example #30
Source File: JpaPageUser.java    From attic-rave with Apache License 2.0 4 votes vote down vote up
/**
* @return the page
*/
@Override
@JsonBackReference
public Page getPage() {
    return page;
}