Search in sources :

Example 1 with JsonObjectBase

use of com.baidu.dsp.common.vo.JsonObjectBase in project disconf by knightliao.

the class MyExceptionHandler method paramError.

/**
     * 参数错误
     *
     * @param paramErrors
     * @param errorCode
     *
     * @return
     */
private ModelAndView paramError(Map<String, String> paramErrors, ErrorCode errorCode) {
    JsonObjectBase jsonObject = JsonObjectUtils.buildFieldError(paramErrors, errorCode);
    LOG.warn(jsonObject.toString());
    return JsonObjectUtils.JsonObjectError2ModelView((JsonObjectError) jsonObject);
}
Also used : JsonObjectBase(com.baidu.dsp.common.vo.JsonObjectBase)

Example 2 with JsonObjectBase

use of com.baidu.dsp.common.vo.JsonObjectBase in project disconf by knightliao.

the class MyExceptionHandler method getParamErrors.

/**
     * TypeMismatchException中获取到参数错误类型
     *
     * @param e
     */
private ModelAndView getParamErrors(TypeMismatchException e) {
    Throwable t = e.getCause();
    if (t instanceof ConversionFailedException) {
        ConversionFailedException x = (ConversionFailedException) t;
        TypeDescriptor type = x.getTargetType();
        Annotation[] annotations = type != null ? type.getAnnotations() : new Annotation[0];
        Map<String, String> errors = new HashMap<String, String>();
        for (Annotation a : annotations) {
            if (a instanceof RequestParam) {
                errors.put(((RequestParam) a).value(), "parameter type error!");
            }
        }
        if (errors.size() > 0) {
            return paramError(errors, ErrorCode.TYPE_MIS_MATCH);
        }
    }
    JsonObjectBase jsonObject = JsonObjectUtils.buildGlobalError("parameter type error!", ErrorCode.TYPE_MIS_MATCH);
    return JsonObjectUtils.JsonObjectError2ModelView((JsonObjectError) jsonObject);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) ConversionFailedException(org.springframework.core.convert.ConversionFailedException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) HashMap(java.util.HashMap) JsonObjectBase(com.baidu.dsp.common.vo.JsonObjectBase) Annotation(java.lang.annotation.Annotation)

Example 3 with JsonObjectBase

use of com.baidu.dsp.common.vo.JsonObjectBase in project disconf by knightliao.

the class WebCommonInterceptor method returnJsonSystemError.

/**
     * 自定义的全局错误
     *
     * @param request
     * @param response
     * @param message
     * @param errorCode
     *
     * @throws IOException
     */
protected void returnJsonSystemError(HttpServletRequest request, HttpServletResponse response, String message, ErrorCode errorCode) throws IOException {
    JsonObjectBase jsonObjectBase = JsonObjectUtils.buildGlobalError(message, errorCode);
    response.setHeader("Cache-Control", "no-cache");
    response.setContentType("application/json;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(JsonUtils.toJson(jsonObjectBase));
}
Also used : JsonObjectBase(com.baidu.dsp.common.vo.JsonObjectBase)

Example 4 with JsonObjectBase

use of com.baidu.dsp.common.vo.JsonObjectBase in project disconf by knightliao.

the class MyExceptionHandler method getParamErrors.

/**
     * 参数转换出错
     *
     * @param e
     *
     * @return
     */
private ModelAndView getParamErrors(InvalidPropertyException e) {
    Map<String, String> errorMap = new HashMap<String, String>();
    errorMap.put(e.getPropertyName(), " parameter cannot find");
    JsonObjectBase jsonObject = JsonObjectUtils.buildFieldError(errorMap, ErrorCode.TYPE_MIS_MATCH);
    return JsonObjectUtils.JsonObjectError2ModelView((JsonObjectError) jsonObject);
}
Also used : HashMap(java.util.HashMap) JsonObjectBase(com.baidu.dsp.common.vo.JsonObjectBase)

Aggregations

JsonObjectBase (com.baidu.dsp.common.vo.JsonObjectBase)4 HashMap (java.util.HashMap)2 Annotation (java.lang.annotation.Annotation)1 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1