當前位置: 首頁>>代碼示例>>Java>>正文


Java MappingJackson2JsonView.setAttributesMap方法代碼示例

本文整理匯總了Java中org.springframework.web.servlet.view.json.MappingJackson2JsonView.setAttributesMap方法的典型用法代碼示例。如果您正苦於以下問題:Java MappingJackson2JsonView.setAttributesMap方法的具體用法?Java MappingJackson2JsonView.setAttributesMap怎麽用?Java MappingJackson2JsonView.setAttributesMap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.web.servlet.view.json.MappingJackson2JsonView的用法示例。


在下文中一共展示了MappingJackson2JsonView.setAttributesMap方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: resolveException

import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入方法依賴的package包/類
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
    HttpServletResponse httpServletResponse, Object o, Exception e) {
  HttpDownServer.LOGGER.error("rest error:",e);
  ModelAndView modelAndView = new ModelAndView();
  try {
    ResultInfo resultInfo = new ResultInfo().setStatus(ResultStatus.ERROR.getCode())
        .setMsg(ResultInfo.MSG_ERROR);
    Map<String, Object> attr = JSON.parseObject(JSON.toJSONString(resultInfo), Map.class);
    MappingJackson2JsonView view = new MappingJackson2JsonView();
    view.setAttributesMap(attr);
    modelAndView.setView(view);
  } catch (Exception e1) {
    e1.printStackTrace();
  }
  return modelAndView;
}
 
開發者ID:monkeyWie,項目名稱:proxyee-down,代碼行數:18,代碼來源:RestExceptionHandler.java

示例2: handlerUnauthenticatedException

import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入方法依賴的package包/類
@ExceptionHandler({OptionalNotPresentException.class})
@ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView handlerUnauthenticatedException(Exception ex, NativeWebRequest request) {
    if ((request.getHeader("accept").contains("application/json"))) {
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        Map<String, Serializable> attributes = new HashMap<>();
        attributes.put("error", HttpStatus.NOT_FOUND);
        view.setAttributesMap(attributes);
        return new ModelAndView(view);
    } else {
        return new ModelAndView("/404");
    }
}
 
開發者ID:izhangzhihao,項目名稱:SSMSeedProject,代碼行數:14,代碼來源:ExceptionHandlerController.java

示例3: handlerExceptionMethod

import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入方法依賴的package包/類
/**
 * 全局Controller異常處理
 *
 * @param ex 異常
 * @return 跳轉出錯頁麵
 */
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handlerExceptionMethod(Exception ex, NativeWebRequest request) {
    if ((request.getHeader("accept").contains("application/json"))) {//如果不是異步請求
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        Map<String, java.io.Serializable> attributes = new HashMap<>();
        attributes.put("error", HttpStatus.INTERNAL_SERVER_ERROR);
        if (env.getDefaultProfiles()[0].equals("development")) {
            attributes.put("MSG", ex.toString());
            attributes.put("Line", ex.getStackTrace()[0].getLineNumber());
            attributes.put("Method", ex.getStackTrace()[0].getMethodName());
        }
        view.setAttributesMap(attributes);
        return new ModelAndView(view);
    } else {
        if (env.getDefaultProfiles()[0].equals("development")) {
            ModelAndView modelAndView = new ModelAndView("500");
            modelAndView.addObject("MSG", ex.toString());
            modelAndView.addObject("Line", ex.getStackTrace()[0].getLineNumber());
            modelAndView.addObject("Method", ex.getStackTrace()[0].getMethodName());
            Writer writer = new StringWriter();
            //異常輸出到瀏覽器console
            ex.printStackTrace(new PrintWriter(writer));
            modelAndView.addObject("detailed", writer.toString());
            return modelAndView;
        } else {
            return new ModelAndView("/500");
        }
    }
}
 
開發者ID:izhangzhihao,項目名稱:SSMSeedProject,代碼行數:37,代碼來源:ExceptionHandlerController.java

示例4: handlerUnauthenticatedException

import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入方法依賴的package包/類
/**
 * 無權限訪問跳轉
 */
@ExceptionHandler({UnauthorizedException.class})
@ResponseStatus(HttpStatus.FORBIDDEN)
public ModelAndView handlerUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
    if ((request.getHeader("accept").contains("application/json"))) {
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        Map<String, java.io.Serializable> attributes = new HashMap<>();
        attributes.put("error", "403");
        attributes.put("cause", HttpStatus.FORBIDDEN);
        view.setAttributesMap(attributes);
        return new ModelAndView(view);
    } else {
        return new ModelAndView("../../403");
    }
}
 
開發者ID:izhangzhihao,項目名稱:SpringMVCSeedProject,代碼行數:18,代碼來源:HandlerExceptionController.java

示例5: handlerExceptionMethod

import org.springframework.web.servlet.view.json.MappingJackson2JsonView; //導入方法依賴的package包/類
/**
 * 全局Controller異常處理
 *
 * @param ex 異常
 * @return 跳轉出錯頁麵
 */
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handlerExceptionMethod(Exception ex, NativeWebRequest request) {

    //將錯誤信息記錄到數據庫
    LogToDB(ex);

    if ((request.getHeader("accept").contains("application/json"))) {//如果不是異步請求
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        Map<String, java.io.Serializable> attributes = new HashMap<>();
        attributes.put("error", "500");
        attributes.put("cause", HttpStatus.INTERNAL_SERVER_ERROR);
        view.setAttributesMap(attributes);
        return new ModelAndView(view);
    } else {
        ModelAndView modelAndView = new ModelAndView("../../500");
        modelAndView.addObject("MSG", ex.toString());
        modelAndView.addObject("Line", ex.getStackTrace()[0].getLineNumber());
        modelAndView.addObject("Method", ex.getStackTrace()[0].getMethodName());
        Writer writer = new StringWriter();
        //客戶端輸出一下,打開F12可以看到
        ex.printStackTrace(new PrintWriter(writer));
        modelAndView.addObject("detailed", writer.toString());
        return modelAndView;
    }
}
 
開發者ID:izhangzhihao,項目名稱:SpringMVCSeedProject,代碼行數:33,代碼來源:HandlerExceptionController.java


注:本文中的org.springframework.web.servlet.view.json.MappingJackson2JsonView.setAttributesMap方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。