SpringBoot接口同时支持form-data、json格式的参数

在项目开发中,遇到对外提供回调接口,好几个平台回调该接口,参数格式也不一样,这时就需要同时支持form-data、json多种格式的参数,如果用原来SpringBoot的方式只能支持一种入参格式。因此,我使用了原生的HttpServletRequest接收参数,再根据数据格式进行处理。

    /**
     * 订单回调
     *
     * @param type - OrderInterface枚举名
     * @return
     */
    @PostMapping(value = "/orderCallback")
    public Object orderCallback(@RequestParam("type") String type, HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        String contentType = oConvertUtils.getString(request.getContentType(), "application/x-www-form-urlencoded");
        if (contentType.contains("application/json")) {
            // json 解析
            params = JSON.parseObject(RequestUtils.getRequestJson(request));
        } else if (contentType.contains("multipart/form-data") || contentType.contains("application/x-www-form-urlencoded")) {
            // form 表单解析
            params = RequestUtils.getRequestParams(request);
        } else {
            throw new RuntimeException("不支持的content-type");
        }
        OrderStrategyService strategyService = orderStrategyFactory.getStrategyService(OrderInterface.getValueByName(type));
        return strategyService.orderCallback(params);
    }

参数解析工具类如下:


import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class RequestUtils {
    /**
     * *把request转换成json数据
     **/
    public static String getRequestJson(HttpServletRequest request) {
        BufferedReader reader = null;
        StringBuilder sb = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

    /**
     * 把request转换成map数据
     **/
    public static Map<String, Object> getRequestParams(HttpServletRequest request) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String[]> paramMap = request.getParameterMap();
        for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
            String paramName = entry.getKey();
            String[] paramValues = entry.getValue();
            params.put(paramName, paramValues[0]); // 只取第一个值
        }
        return params;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅、途

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值