Spring boot 自定义参数转换类

1,345 阅读1分钟

具体转换类

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


public class ControlConverter implements Converter<String, Control> {


    @Override
    public Control convert(String s) {
        JSONObject jsonObject = JSONObject.parseObject(s);
        Control control = new Control();
        control.setDeviceID(jsonObject.getString("deviceID"));
        control.setDeviceType(jsonObject.getInteger("deviceType"));
        control.setManufactureCode(jsonObject.getString("manufactureCode"));
        String commandName = jsonObject.getString("commandName");
        JSONObject commandJson = jsonObject.getJSONObject("t");
        Command t = null;
        if ("HornCommand".equals(commandName)) {
            t = commandJson.toJavaObject(HornCommand.class);
        }
        if ("ScreenCommand".equals(commandName)) {
            t = commandJson.toJavaObject(ScreenCommand.class);
        }
        if ("LiftCommand".equals(commandName)) {
            t = commandJson.toJavaObject(LiftCommand.class);
        }
        if ("BarrierCommand".equals(commandName)) {
            t = commandJson.toJavaObject(BarrierCommand.class);
        }
        control.setT(t);
        return control;
    }

}

注解,标识指定方法

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConvertInterface {
}

方法参数转换入口

public class MethodArgumentResolver implements HandlerMethodArgumentResolver {

    Map<String, Converter> converters = new ConcurrentHashMap<>();

    public void addConverters(String name, Converter converter) {
        converters.put(name, converter);
    }


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        ConvertInterface convertInterface = methodParameter.getParameterAnnotation(ConvertInterface.class);
        return convertInterface != null;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
        Method method = (Method) parameter.getMember();
        Class[] classes = method.getParameterTypes();
        String param = resolveRequestBody(request);
        Converter converter = converters.get(classes[0].getName());
        if (converter == null) {
            return null;
        }
        return converter.convert(param);
    }

    private String resolveRequestBody(ServletRequest request) {
        BufferedReader reader = null;
        try {
            reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }
}

配置方法参数转换类

  @Bean
    public WebMvcConfigurer WebMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
                MethodArgumentResolver methodArgumentResolver = new MethodArgumentResolver();
                methodArgumentResolver.addConverters(Control.class.getName(), new ControlConverter());
                resolvers.add(methodArgumentResolver);
            }
        };
    }