ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

IBaseEnum.java

public interface IBaseEnum {
    public String getName();
}

FuncEnum.java

import com.ssslinppp.enumConvert.IBaseEnum;

public enum FuncEnum implements IBaseEnum {
    AVG("avg", "func-avg"),
    MAX("max", "func-max"),
    MIN("min", "func-min"),
    SUM("sum", "func-sum"),
    LAST("last", "func-last");

    private String name;
    private String desc;

    FuncEnum(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}


StringToBaseEnumConverterFactory.java

package com.ssslinppp.enumConvert;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

/**
 * Description: 将String转换为枚举类型
 */
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, IBaseEnum> {
    @Override
    public <T extends IBaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
        if (!targetType.isEnum()) {
            throw new UnsupportedOperationException("只支持转换到枚举类型");
        }
        return new StringToBaseEnumConverter(targetType);
    }

    private class StringToBaseEnumConverter<T extends IBaseEnum> implements Converter<String, T> {
        private final Class<T> enumType;

        public StringToBaseEnumConverter(Class<T> enumType) {
            this.enumType = enumType;
        }

        @Override
        public T convert(String s) {
            for (T t : enumType.getEnumConstants()) {
                if (s.equals(t.getName())) {
                    return t;
                }
            }
            return null;
        }
    }
}


注册自定义的ConverterFactory

MyWebAppConfigurer.java

package com.ssslinppp.configs;

import com.ssslinppp.enumConvert.StringToBaseEnumConverterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Description:注册自定义的ConverterFactory
 */
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new StringToBaseEnumConverterFactory());
    }
}


测试

EnumModel.java

package com.ssslinppp.model;

import com.ssslinppp.enumConvert.baseEnumImpl.FuncEnum;

public class EnumModel {
    private FuncEnum funcEnum;

    public FuncEnum getFuncEnum() {
        return funcEnum;
    }

    public void setFuncEnum(FuncEnum funcEnum) {
        this.funcEnum = funcEnum;
    }
}

同时测试@RequestBody和@RequestParam

EnumController.java 测试类

package com.ssslinppp.controller;

import com.ssslinppp.enumConvert.baseEnumImpl.FuncEnum;
import com.ssslinppp.model.EnumModel;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/enum")
public class EnumController {

    @RequestMapping("/print")
    public String printEnums(@RequestBody EnumModel enumModel, @RequestParam("pathEnum") FuncEnum pathEnum) {
        System.out.println("[" + enumModel.getFuncEnum().getName() + ":" + enumModel.getFuncEnum().getDesc() + "]");
        System.out.println("[" + pathEnum.getName() + ":" + pathEnum.getDesc() + "]");
        return "[" + pathEnum.getName() + ":" + pathEnum.getDesc() + "]";
    }
}

请求URL

localhost:8080/enum/print?pathEnum=max
method: post
mediaType:application

请求体:
{
    "funcEnum": "AVG"
}

测试结果(output):

[avg:func-avg]
[max:func-max]

参考:
Demo1
Spring Boot绑定枚举类型参数

posted on 2017-06-30 21:38  ssslinppp  阅读(7266)  评论(0编辑  收藏  举报