首页>代码>spring mvc开发实现图片动态裁剪上传,并配置多数据源示例>/spring/springmvc/src/main/java/com/it/controller/DefaultController.java
package com.it.controller;


import com.alibaba.fastjson.JSONObject;
import com.it.constant.ResultKey;
import com.it.exception.AuthException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;

@Controller
public class DefaultController {
  protected static Logger LOGGER = LoggerFactory.getLogger(DefaultController.class);

  protected HttpHeaders httpHeaders = new HttpHeaders();

  @Resource
  protected ServletContext servletContext;

  @Resource
  protected HttpServletRequest httpServletRequest;

  @Resource
  protected HttpServletResponse httpServletResponse;

  public DefaultController() {
    MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));
    httpHeaders.setContentType(mediaType);
  }

  @ExceptionHandler(value = {Exception.class})
  public ResponseEntity<String> exceptionHandler(Exception ex) {
    JSONObject result = new JSONObject();

    if (ex instanceof AuthException) {
      LOGGER.debug("error message = {}, data = {}", ex.getMessage(), ((AuthException) ex).getData());
    } else {
      ex.printStackTrace();
    }

//    if (ex instanceof InvalidSessionException) {
//      result.put("invalidSession", true);
//    }

    result.put(ResultKey.success.name(), false);
    result.put("message", ex.getMessage());

    return new ResponseEntity<String>(result.toJSONString(), httpHeaders, HttpStatus.OK);
  }

  /**
   * 简化操作, 生成字符串为:
   * { "success":true, "data": obj }
   */
  protected String successDataToJSONString() {
    JSONObject result = new JSONObject();
    result.put(ResultKey.success.name(), true);
    return result.toJSONString();
  }

  /**
   * 简化操作, 生成字符串为:
   * { "success":true, "data": obj }
   */
  protected String successDataToJSONString(Object obj) {
    JSONObject result = new JSONObject();
    result.put(ResultKey.success.name(), true);
    result.put(ResultKey.data.name(), obj);
    return result.toJSONString();
  }
}
最近下载更多
rookie02  LV7 2020年4月28日
zywzywzyw1  LV17 2019年10月29日
po59012  LV1 2019年8月20日
LHJ123  LV30 2019年4月18日
pengboss  LV12 2019年4月2日
skipple3  LV39 2019年2月13日
keen1066  LV17 2018年12月25日
LIYUANWAI  LV10 2018年11月23日
zhouxiaoyu  LV7 2018年11月14日
haiwen  LV19 2018年11月8日
最近浏览更多
softandroad 2022年11月8日
暂无贡献等级
zhy1989wz  LV6 2022年3月15日
444105047  LV6 2021年12月3日
18728748707  LV13 2021年10月12日
dududufeidad  LV9 2021年10月11日
upupup  LV5 2021年6月11日
13427720820  LV8 2021年5月4日
羞羞小子  LV13 2021年4月9日
ghost_M  LV7 2021年1月7日
lario001  LV2 2020年12月1日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友