All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.happyjiahui.z.web.controller.BaseController Maven / Gradle / Ivy

There is a newer version: 0.1.29
Show newest version
package com.github.happyjiahui.z.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.ValidationException;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import com.alibaba.fastjson.JSON;

/**
 * controller基类,提供参数校验等功能。
 * 
 * @author zhaojinbing
 * @version 0.1
 */
public class BaseController {

    /**
     * 校验参数
     * 
     * @param result
     *            {@link BindingResult}
     */
    protected void checkBindingResult(BindingResult result) {
        if (result.hasErrors()) {
            List> errorMapList = new ArrayList<>();
            for (FieldError fieldError : result.getFieldErrors()) {
                Map errorMap = new HashMap<>();
                errorMap.put("field", fieldError.getField());
                errorMap.put("message", fieldError.getDefaultMessage());
                errorMapList.add(errorMap);
            }
            throw new ValidationException(JSON.toJSONString(errorMapList));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy