SpringMVC自定义 Resolver疑问

1.SpringMVC可以自定义Resolver完成某些参数的解析,加入我想实现一个参数轮流被2个Resolver解析,Spring支持吗?
例如:

@RequestMapping(method = RequestMethod.GET)
   public String test(@RequestParam("title") @UpperCase String text) {
   System.out.println(text);
   return "form";
}

在被RequestParam的resolver解析之后,接着进入我自己写的uppercase的resolver中,我测试了是不行的,好像只支持一种解析,有什么途径可以支持2个Resolver轮流解析吗?

阅读 272
评论 2019-07-09 提问
    1 个回答
    leftstick
    • 26.4k

    可以的哦,试试看:

    先定义一个自己的@UpperCase:

    import java.lang.annotation.*;
    
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
    public @interface UpperCase {
    
    }

    在定义一个formatter的工厂

    
    import org.springframework.format.AnnotationFormatterFactory;
    import org.springframework.format.Formatter;
    import org.springframework.format.Parser;
    import org.springframework.format.Printer;
    
    import java.text.ParseException;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Locale;
    import java.util.Set;
    
    public class UpperAnnotationFormatterFactory implements AnnotationFormatterFactory<UpperCase> {
    
        @Override
        public Set<Class<?>> getFieldTypes() {
            Set<Class<?>> fieldTypes = new HashSet<>();
            fieldTypes.add(String.class);
            return Collections.unmodifiableSet(fieldTypes);
        }
    
        @Override
        public Printer<String> getPrinter(UpperCase annotation, Class<?> fieldType) {
            return configureFormatterFrom(annotation);
        }
    
        @Override
        public Parser<String> getParser(UpperCase annotation, Class<?> fieldType) {
            return configureFormatterFrom(annotation);
        }
    
        private Formatter<String> configureFormatterFrom(UpperCase annotation) {
           
            return new Formatter<String>() {
                @Override
                public String print(String object, Locale locale) {
                    return object;
                }
    
                @Override
                public String parse(String text, Locale locale) throws ParseException {
                    return text.toUpperCase();
                }
            };
        }
    }

    最后,注册一下上面这个工厂

    import org.springframework.context.annotation.Configuration;
    import org.springframework.format.FormatterRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class WebConfigurer implements WebMvcConfigurer {
        @Override
        public void addFormatters(FormatterRegistry registry) {
            registry.addFormatterForFieldAnnotation(new UpperAnnotationFormatterFactory());
        }
    }

    现在你再看看,你的代码

    @RequestMapping(method = RequestMethod.GET)
       public String test(@RequestParam("title") @UpperCase String text) {
       System.out.println(text); // 这个地方的text就已经被大写了
       return "form";
    }
    评论 赞赏 2019-07-22
      撰写回答

      登录后参与交流、获取后续更新提醒