如何在 Olingo Odata4 java spring boot 响应中排除属性

问题描述 投票:0回答:1

如何在响应中排除 Olingo Odata4 java spring boo 中的属性? 例如,在下面的示例中需要在响应中排除“详细信息”属性

//Employee.java  

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;


    @Entity
    @Table(schema="EMPLOWNER", name="EMPLOYEE")
    public class Employee{
    private String empId;
    private String empName;
    // Need to exclude in response with an annotation
    private String details;
    }

以下是使用 Olingo 设置 CsdlProperty 的代码:
EmployeeCsdlEntityType.java

import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.provider.CsdlAnnotation;
import org.apache.olingo.commons.api.edm.provider.CsdlEntityType;
import org.apache.olingo.commons.api.edm.provider.CsdlProperty;
import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef;
import org.apache.olingo.commons.api.edm.provider.annotation.CsdlConstantExpression;

public class EmplCsdlEntityType implements ICsdlEntityType{

@Override
public CsdlEntityType getCsdlEntityType(EntityDetailEnum entityDetailEnum){
CsdlProperty empId=new CsdlProperty().setName("empId").setType(EdmPrimitiveTypeKind.String.getFullQulifiedName());
CsdlProperty empName=new CsdlProperty().setName("empName").setType(EdmPrimitiveTypeKind.String.getFullQulifiedName());
CsdlProperty details=new CsdlProperty().setName("details").setType(EdmPrimitiveTypeKind.String.getFullQulifiedName());

CsdlPropertyRef propRef=new CsdlPropertyRef();
propRef.setName("Id");

CsdlEntityType entityType=new CsdlEntityType();
entityType.setName(entityDetailEnum.getEntityType());
entityType.setProperties(Arrays.asList(empId,empName,details))
entityType.setKey(Collections.singletonList(propertyRefOwner));
return entityType;
}
}
java spring-boot odata olingo
1个回答
0
投票

我可以通过创建自定义注释并验证注释来解决这个问题:

//Exclude.java
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Exclude{
    String name() default "";
    }

//URIValidation.java
public class URIValidation{
public static void validatePropertyExcluded(UriInfo uriInfo, IGetEntityData entityDataClass)throws Exception{
String filterField = null;
try{
FilterOption filterOption=uriInfo.getFilterOption();
if(filterOption !=null){
List<String> leftOperands = new ArrayList<>();
BinaryImpl binaryImpl=(BinaryImpl)filterOption.getExpression();
collectOperands(binaryImpl,leftOperands);
ParamerterizedType pt = (ParameterizedType) entityDataClass.getClass().getGenericInterfaces()[0];
for(Field field: entityClass.getDeclaredFields()){
Annotation[] annotations = field.getDeclaredAnnotationsByType(Exclude.class);
if(annotations.length >0){
if(!leftOperands.isEmpty() && leftOperands.contains(field.getName())){
throw new ODataRuntimeException(Given Filkter Field " +filed.getName() + " Not Found or Not Supported");
}
}
}
}catch(Exception e){
e.printStackTrace();
throw e;
}

}

private static void collectOperands(VisitableExpression expression, List<String> leftOperands){
if(expression instanceof BinaryImpl){
BinaryImpl binary=(BinaryImpl) expression;
collectOperands(binary.getLeftOperand(), leftOperands);
collectOperands(binary.getRightOperand(), leftOperands);
}else if(expression instanceof MemberImpl){
MemberImpl member = (MemberImpl) expression;
String field = member.toString().replaceAll("\\[\\]","");
leftOperands.add(field);
}
}
}

//EntityCollectionProcessorImpl.java
public void readEntityCollection(){
...
URIValidation.validatePropertyExcluded(uriInfo,entityDataObject);
...
}
© www.soinside.com 2019 - 2024. All rights reserved.