下面列出了怎么用com.fasterxml.jackson.annotation.JsonBackReference的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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()));
}
/**
* 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;
});
}
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;
}
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;
}
@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);
}
@JsonBackReference
public Process getParentProcess() {
return parentProcess;
}
@JsonBackReference
public TUser getUser() {
return user;
}
@XmlTransient
@JsonBackReference("paymentAp")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "paymentAp")
public Set<Invoice> getInvoices() {
return this.invoices;
}
@XmlTransient
@JsonBackReference("paymentAp")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "paymentAp")
public Set<Invoice> getInvoices() {
return this.invoices;
}
@JsonBackReference("trips-data")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "stationDetail")
public Set<Trip> getTrips() {
return this.trips;
}
@JsonBackReference("customer-data")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customerInfo")
public Set<BookedTrip> getBookedTrips() {
return this.bookedTrips;
}
@JsonBackReference("trips-data")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "stationId", nullable = false)
public StationDetail getStationDetail() {
return this.stationDetail;
}
@JsonBackReference("booked-data")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "trip")
public Set<BookedTrip> getBookedTrips() {
return this.bookedTrips;
}
@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;
}
@JsonBackReference
@NotNull
public Menu getParent() {
return parent;
}
@JsonBackReference
public Process getParentProcess() {
return parentProcess;
}
@JsonBackReference
public void setParent(Configurable parent) {
this.parent = parent;
}
@JsonManagedReference
@JsonIgnore
@JsonBackReference
IMAPMessage getFolder();
@JsonManagedReference
@JsonIgnore
@JsonProperty("parent")
@JsonBackReference
MimeMultipart getParent();
@Override
@JsonBackReference
public Page getPage() {
return page;
}
/**
* Gets the associated region
*
* @return the region
*/
@Override
@JsonBackReference
public Region getRegion() {
return region;
}
/**
* @return the page
*/
@Override
@JsonBackReference
public Page getPage() {
return page;
}
/**
* Gets the associated page
*
* @return the associated page
*/
@Override
@JsonBackReference
public Page getPage() {
return page;
}
@JsonBackReference
Region getRegion();
/**
* 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);
}
@Test
public void shouldGetExcludeAnnotationsConverter() {
apiBundle.init(container, bundleConfiguration);
ExcludeAnnotationsConvertor convertor = apiBundle.getExcludeAnnotationsConvertor();
assertEquals(convertor.getExcludeAnnotations(), Lists.newArrayList(JsonBackReference.class, JsonIgnore.class));
}
/**
* @return the field2
*/
@JsonBackReference
public String getField2() {
return field2;
}
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() );
}
@JsonBackReference
public SimpleTreeNode2 getParent() {
return parent;
}
@JsonBackReference
public Parent getParent() {
return parent;
}