Java源码示例:org.springframework.web.servlet.view.json.MappingJacksonJsonView

示例1
@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
@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
@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
@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
@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
@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;
}