我有以下json:
{
"managedObject": {
"uid": "00c7a5ac-5653-3ed6-9cf6-667d0258ceae",
"attributes": {
"vsDataEUtranCellRelation": {
"amoState": "ENABLED"
}}}}
用户可以指定要返回的字段。 在此示例中:
此请求的结果是:
"managedObject": {
"uid": "00c7a5ac-5653-3ed6-9cf6-667d0258ceae",
"attributes": {}
}}
不包含:attribute.vsDataEUtranCellRelation.amoState
具有以下字段:
响应为:
{
"managedObject": {
"uid": "00c7a5ac-5653-3ed6-9cf6-667d0258ceae",
"attributes": {
"vsDataEUtranCellRelation": {
"amoState": "ENABLED",
"eUtranCellRelationId": "024886_1_2",
"eranExternalUlCompGroupAvail": "ENABLED",
"ieNBUlCompCoopCellAllowed": "true",
"includeInSystemInformation": "true",
"isHoAllowed": "true",
"isRemoveAllowed": "false",
"lbCovIndicated": "false",
"neighborCellRef": "3",
"qOffsetCellEUtran": "0",
"sleepModeCovCellCandidate": "AUTO",
"sleepModeCoverageCell": "false"
}}}}
负责处理过滤器请求的代码为:
public class RestResponseFilter extends AbstractMappingJacksonResponseBodyAdvice {
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RestResponseIncludes {
String fields();
}
public static final String FILTER_ID = "RestResponseFilter";
public static final String PARAM_NAME = "fields";
private static final String LEAN_REPRESENTATION = "__LEAN_OBJECT__";
private static final String DELIMITER = ",";
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer,
org.springframework.http.MediaType contentType, MethodParameter returnType,
ServerHttpRequest request, ServerHttpResponse response) {
if (bodyContainer.getFilters() != null ) {
return;
}
HttpServletRequest baseReq = ((ServletServerHttpRequest) request).getServletRequest();
String include = baseReq.getParameter(PARAM_NAME);
PropertyFilter filter = SimpleBeanPropertyFilter.serializeAll();
if (include != null && !include.isEmpty()) {
if (include.contains(LEAN_REPRESENTATION)) {
Object temp = bodyContainer.getValue();
if (temp instanceof Collection){
temp = ((Collection) temp).stream().findAny().orElse(null);
}
if (temp != null) {
RestResponseIncludes restResponseIncludes = temp.getClass().getAnnotation(RestResponseIncludes.class);
if (restResponseIncludes != null) {
String mandatory = restResponseIncludes.fields();
if (mandatory != null && !mandatory.isEmpty()) {
include = include.replace(LEAN_REPRESENTATION, mandatory);
}
}
}
}
String[] attrs = include.split(DELIMITER);
filter = RestBeanPropertyFilter.filterOutAllExcept(attrs);
}
bodyContainer.setFilters(new SimpleFilterProvider().addFilter(FILTER_ID, filter));
}
}
您知道我缺少为复杂的子文档启用过滤功能的想法吗?