无法过滤和自定义对子子文档中的属性的响应的属性

时间:2019-06-26 16:44:49

标签: spring spring-boot spring-mvc jackson

我有以下json:

{
"managedObject": {
"uid": "00c7a5ac-5653-3ed6-9cf6-667d0258ceae",
"attributes": {
   "vsDataEUtranCellRelation": { 
              "amoState": "ENABLED"
}}}}

在我的API请求中: http://localhost:9091/naas/v1/mos/00c7a5ac-5653-3ed6-9cf6-667d0258ceae?fields=uid%2Cattributes.vsDataEUtranCellRelation.amoState&links=false

用户可以指定要返回的字段。 在此示例中:

  1. uid
  2. attributes.vsDataEUtranCellRelation.amoState

此请求的结果是:

"managedObject": {
"uid": "00c7a5ac-5653-3ed6-9cf6-667d0258ceae",
"attributes": {}
}}

不包含:attribute.vsDataEUtranCellRelation.amoState

当我尝试此请求时: http://localhost:9091/naas/v1/mos/00c7a5ac-5653-3ed6-9cf6-667d0258ceae?fields=uid%2Cattributes.vsDataEUtranCellRelation&links=false

具有以下字段:

  1. uid
  2. attributes.vsDataEUtranCellRelation

响应为:

{
"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));
    }
}

您知道我缺少为复杂的子文档启用过滤功能的想法吗?

0 个答案:

没有答案