org.springframework.web.servlet.view.json.MappingJacksonJsonView Java Examples

The following examples show how to use org.springframework.web.servlet.view.json.MappingJacksonJsonView. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExceptionHandler.java    From onboard with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source File: ExceptionResolver.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #3
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example #4
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example #5
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}
 
Example #6
Source File: WebMvcConfig.java    From maven-framework-project with MIT License 5 votes vote down vote up
@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;
}