Solon v2.7.6

特珠的参数名风格模拟成 json 请求

</> markdown

当有 Properties 风格的表单参数名时,框架不能正常解析。比如:

  • user.name=solon
  • user.addres[0]=aaa
  • user.addres[1]=bb

可借用工具把它转换 json 格式,再模板成 json 请求:

@Component
public class RouterInterceptorImpl implements RouterInterceptor {
    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
        if (mainHandler != null && ctx.isMultipart() == false && ctx.paramsMap().size() > 0) {
            //把表单数据转成 json
            Properties properties = new Properties();
            properties.putAll(ctx.paramsMap());
            
            //如果格式更复杂,可能要自己写工具转?!
            String json = ONode.load(properties).toJson(); //ONode 支持将 Properties 的数据,转换有结构的 json

            //重置 body 内容及内容类型
            ctx.bodyNew(json);
            ctx.headerMap().put("Content-type", "application/json");
        }

        chain.doIntercept(ctx, mainHandler);
    }
}

仅供小参考!