【问题标题】:Jackson JSON View - exclude view variables from the output (Spring MBV)Jackson JSON View - 从输出中排除视图变量(Spring MBV)
【发布时间】:2017-08-15 16:45:57
【问题描述】:

所以我正在学习 Spring MVC 框架并使用 MappingJackson2JsonView 类设置了内容协商,这样当我转到 /products 时,我会得到正常的 HTML 视图,而当我转到 /producs.json 时,我会得到一个 JSON模型的 - 这很好用。 我的问题是,如何从 JSON 输出中排除变量?我希望排除的这些变量是由我创建的拦截器设置的,用于向我向 HTML 用户显示的模型添加属性;

拦截器:

public class GlobalVariablesInterceptor extends HandlerInterceptorAdapter {

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    if (modelAndView != null) {
        ModelMap model = modelAndView.getModelMap();
        model.addAttribute("cp", request.getServletContext().getContextPath());

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (!(auth instanceof AnonymousAuthenticationToken)) {
            Set<String> roles = auth.getAuthorities().stream().map(r -> r.getAuthority()).collect(Collectors.toSet());
            String userRoles = String.join(",", roles);
            model.addAttribute("roles", userRoles);
            model.addAttribute("authUsername", auth.getName());
        }

    }
}
}

JSON 豆:

@Bean
public MappingJackson2JsonView jsonView() {
    MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
    jsonView.setPrettyPrint(true);
    return jsonView;
}

@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
    ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
    resolver.setContentNegotiationManager(manager);
    ArrayList<View> views = new ArrayList<>();
    views.add(jsonView());
    views.add(xmlView());
    resolver.setDefaultViews(views);
    return resolver;
}

任何帮助将不胜感激。

【问题讨论】:

    标签: java spring-mvc jackson


    【解决方案1】:

    因此,为了将来阅读此内容的任何人的利益......

    我尝试过的事情:

    • 设置明确的模型变量列表 向前,在 WebContextConfig 的 bean 定义中执行此操作。 没用。
    • 在 postHandle 中查询 ModelAndView 对象 拦截器的方法;试图查看视图是否是 MappingJackson2JsonView 的实例;没用。
    • 查询postHandle方法的HttpServletResponse对象获取其 内容类型并查看它是否是应用程序/json(不起作用... null 指针异常)等

    确实有效的一件事(但显然不是非常“弹性”的代码)....查询 postHandle 方法的 HttpServletRequest 对象,查看请求的 URL 是否包含 .json(这是 JSON 视图的触发器)...如果是,那么首先不要将这些额外的变量添加到模型中:

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        if (modelAndView != null) {
            String url = request.getRequestURI().toLowerCase();
            if (!(url.contains(".json") || url.contains(".xml"))) {
                ModelMap model = modelAndView.getModelMap();
                model.addAttribute("cp", request.getServletContext().getContextPath());
    
                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
                if (!(auth instanceof AnonymousAuthenticationToken)) {
                    Set<String> roles = auth.getAuthorities().stream().map(r -> r.getAuthority()).collect(Collectors.toSet());
                    String userRoles = String.join(",", roles);
                    model.addAttribute("roles", userRoles);
                    model.addAttribute("authUsername", auth.getName());
                }
    
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 2019-02-05
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多