BeanUtils StringUtils Validation

package com.example.demo.BeanUtils;


import com.example.demo.dao.Animals;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shuyue.guo
 * @date 2019/12/16
 */

public class test {

    public void test1() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "tuantuan");
        map.put("age", 3);
        map.put("color", "black");
        map.put("sex", "female");

        // 将map数据拷贝到javabean中
        Animals a1 = new Animals();
        BeanUtils.populate(a1,map);
        System.out.println(a1);

        // 对象的拷贝
        Animals a2 = new Animals();
        org.springframework.beans.BeanUtils.copyProperties(a1,a2);

        System.out.println(a2);

        Animals a3 = new Animals();

        // 设置指定的属性
        BeanUtils.setProperty(a3, "sex", "male");
        System.out.println("set"+a3);

        // 拷贝指定的属性
        Animals a4 = new Animals();

        BeanUtils.copyProperty(a4, "sex", "22");
        System.out.println("copy" + a4);
        System.out.println("copy"+a3);


        // 克隆对象
        Object object = BeanUtils.cloneBean(a3);
        System.out.println(object);

        // 将整型转换成字符串
        Object objectConvert = ConvertUtils.convert(123,String.class);
        String typeName = object.getClass().getTypeName();
        System.out.println(typeName);

        // 将日期转换成字符串
        Object object2 = ConvertUtils.convert(new Date(), String.class);
        String typeName2 = object2.getClass().getTypeName();
        System.out.println(typeName2);

        // 将字符串转换成Double
        Object object3 = ConvertUtils.convert("123", Double.class);
        String typeName3 = object3.getClass().getTypeName();
        System.out.println(typeName3);

        // 自定义convert使用
        MyConverter myConverter = new MyConverter("yyy-MM-dd HH:mm:ss");
        //注册该转换器
        ConvertUtils.register(myConverter,Date.class);
        Animals a5 = new Animals();
        Object object4 = ConvertUtils.convert("2019-03-13 14:04:00",Date.class);
        System.out.println(object4.getClass().getTypeName());
        System.out.println("date:"+object4);

        Animals a6 = new Animals();
        BeanUtils.copyProperty(a6,"birth","2019-03-13 14:04:00");
        System.out.println(a6);

    }

}

状态转换器

package com.example.demo.BeanUtils;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author shuyue.guo
 * @date 2019/12/17
 */
public class MyConverter implements Converter {
    private static SimpleDateFormat format;

    MyConverter(String pattern){
        format = new SimpleDateFormat(pattern);
    }
    @Override
    public Object convert(Class type,Object value) {
        if(value == null){
            return null;
        }else {
            if(value instanceof String){
                String temp = (String)value;
                if(temp.trim().length() == 0){
                    return null;
                }else {
                    try {
                        return format.parse(temp);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }else {
                throw new ConversionException("not string");
            }
        }
        return value;
    }

}

ApiBenaUtils API

StringUtils API

Validation API

获取validation message 并且放在返回api中

package com.example.demo.util;

import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.List;

/**
 * @author shuyue.guo
 * @date 2019/12/11
 */
public class ValidateUtil {
    public static Object validateError(BindingResult result) {
        HashMap<String,Object> msg = new HashMap<>();
        if (result.hasErrors()) {
            List<ObjectError> list = result.getAllErrors();
            for (ObjectError error : list) {
                return msg.put("warn", ValidateUtil.validateError(result));
            }
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值