zoukankan      html  css  js  c++  java
  • SpringBoot 接收 单个String入参之解决方案

    场景:

      在做接口时,有的时候,接口入参只需要一个参数,如果将一个参数封装成一个对象很麻烦,故有了以下方式:

    思路:

      spring自带的参数解析器貌似是不具备这个能力的,所有自定义

    方式方法:

      1.定义一个注解

    @Target(ElementType.PARAMETER)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface RequestSingleParam {
        String value();
    
        boolean required() default true;
    
        String defaultValue() default "";
    }

      2.定义解析器

    public class RequestSingleParamHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
        @Override
        public boolean supportsParameter(MethodParameter parameter) {
            return parameter.hasParameterAnnotation(RequestSingleParam.class);
        }
    
    
        @Override
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                      NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
            RequestSingleParam requestSingleParam = parameter.getParameterAnnotation(RequestSingleParam.class);
            HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            char[] buf = new char[1024];
            int rd;
            while ((rd = reader.read(buf)) != -1) {
                sb.append(buf, 0, rd);
            }
            JSONObject jsonObject = JSONObject.parseObject(sb.toString());
            String value = requestSingleParam.value();
            return jsonObject.get(value);
        }
    
    }

      3.修改Springboot启动类(a.继承WebMvcConfigurerAdapter;b.重写addArgumentResolvers()方法)

    @SpringBootApplication
    @EnableWebMvc
    public class SpringbootDemoApplication extends WebMvcConfigurerAdapter {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootDemoApplication.class, args);
        }
    
        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){
            argumentResolvers.add(new RequestSingleParamHandlerMethodArgumentResolver());
            super.addArgumentResolvers(argumentResolvers);
        }
    }

      4.在Controller中应用 

      调用接口方,传参如: {“name”:"张三"},Controller中就能获取到"张三”

        @RequestMapping(value = "/test",method = RequestMethod.POST)
        public void test(@RequestSingleParam(value = "name") String name){
                System.out.println(name);
        }

      

  • 相关阅读:
    Java中单态设计模式
    Google地图,Baidu地图数据供应商
    栅格数据和矢量数据
    Java学习之Iterator(迭代器)的一般用法 (转)
    highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
    relative、absolute和float
    【转】20个令人敬畏的jQuery插件
    启动tomcat显示成功,但是访问tomcat首页是404错误页
    手机测试Android程序
    Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
  • 原文地址:https://www.cnblogs.com/swugogo/p/9885509.html
Copyright © 2011-2022 走看看