类org.springframework.web.servlet.view.json.MappingJacksonJsonView源码实例Demo

下面列出了怎么用org.springframework.web.servlet.view.json.MappingJacksonJsonView的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: onboard   文件: ExceptionHandler.java
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {
    ModelAndView mav = super.doResolveException(request, response, handler, ex);

    if (ex instanceof NoPermissionException) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        logger.info(String.valueOf(response.getStatus()));
    } else if (ex instanceof BadRequestException) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
    } else if (ex instanceof NoLoginException) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
    } else if (ex instanceof ResourceNotFoundException || ex instanceof InvitationTokenExpiredException
            || ex instanceof InvitationTokenInvalidException || ex instanceof RegisterTokenInvalidException
            || ex instanceof ResetPasswordTokenInvalidException) {
        response.setStatus(HttpStatus.NOT_FOUND.value());
    } else {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }

    mav.setView(new MappingJacksonJsonView());
    mav.addObject("exception", ex);
    logger.debug("view name = {}", mav.getViewName());

    return mav;
}
 
源代码2 项目: mmall-kay-Java   文件: ExceptionResolver.java
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    //打印异常日志
    log.error("{} Exception:",httpServletRequest.getRequestURI(),e);
    //此处因为本项目使用Jackson的包为1.9版本,若使用2.x版本要使用MappingJackson2JsonView
    ModelAndView modelAndView = new ModelAndView(new MappingJacksonJsonView());
    //返回与ServerResponse封装相同的格式
    modelAndView.addObject("status", ResponseCode.ERROR.getCode());
    modelAndView.addObject("msg", "接口发生异常,详情请查看服务器日志");
    modelAndView.addObject("data", e.toString());
    return modelAndView;
}
 
源代码3 项目: maven-framework-project   文件: WebMvcConfig.java
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
源代码4 项目: maven-framework-project   文件: WebMvcConfig.java
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
源代码5 项目: maven-framework-project   文件: WebMvcConfig.java
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
源代码6 项目: maven-framework-project   文件: WebMvcConfig.java
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
    ContentNegotiatingViewResolver result = new ContentNegotiatingViewResolver();
    Map<String, String> mediaTypes = new HashMap<String, String>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON_VALUE);
    result.setMediaTypes(mediaTypes);
    MappingJacksonJsonView jacksonView = new MappingJacksonJsonView();
    jacksonView.setExtractValueFromSingleKeyModel(true);
    Set<String> modelKeys = new HashSet<String>();
    modelKeys.add("events");
    modelKeys.add("event");
    jacksonView.setModelKeys(modelKeys);
    result.setDefaultViews(Collections.singletonList((View) jacksonView));
    return result;
}
 
 类方法
 同包方法