@RequestParam、@RequestBody的坑

hsf构建的项目需要调用 springCloud @EnableFeignClients 的接口。
接口指定了method = RequestMethod.POST
调用方式:


import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.ImmutableMap;
import com.noob.Response;
import com.noob.HttpClientUtil;

@Service
@Slf4j
public class MicroServiceRemote {
    private static final String                UTF_8  = "UTF-8";

    protected static final Map<String, String> HEADER = ImmutableMap.of("Content-Type",
                                                              MediaType.APPLICATION_JSON_VALUE);

    protected <T> Response<T> doRequestParams(Map<String, String> params, String callUrl) {
        log.info("调用【{}】 begin. 参数:{}", callUrl, JSON.toJSONString(params));
        Response<T> result = null;
        try {
            result = JSON.parseObject(HttpClientUtil.doRequestQueryString(callUrl, params, HEADER, UTF_8),
                    new TypeReference<Response<T>>() {
                    });
        } catch (Exception e) {
            log.error("调用【{}】 异常", callUrl, e);
        }
        return result;
    }

    /**
     * @param param 单个简单参数的值
     * @return
     */
    protected <T> Response<T> doRequestJson(Object obj, String callUrl) {
        String requestJson = "";
        if (obj != null) {
            if (obj instanceof String) {
                requestJson = String.class.cast(obj);
            } else {
                requestJson = JSON.toJSONString(obj);
            }
        }

        log.info("调用【{}】 begin. 参数:{}", callUrl, requestJson);
        Response<T> result = null;
        try {
            result = JSON.parseObject(HttpClientUtil.doJsonRequest(callUrl, requestJson, HEADER, UTF_8),
                    new TypeReference<Response<T>>() {
                    });
        } catch (Exception e) {
            log.error("调用【{}】 异常", callUrl, e);
        }
        return result;
    }

}

HttpClienUtil:

    private static String executePostQueryString(String url, Map<String, String> postData, String encoding, Header[] headers)
            throws Exception {
        String responseString = "";
        url = url.trim();
        if (MapUtils.isNotEmpty(postData)) {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> entry : postData.entrySet()) {
                if (sb.length() > 0) {
                    sb.append("&");
                }
                sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));
            }
            sb.insert(0, url + "?");
            url = sb.toString();
        }

        PostMethod postRequest = new PostMethod(url);
        if (headers != null) {
            for (int i = 0; i < headers.length; i++) {
                postRequest.setRequestHeader(headers[i]);
            }
        }

        try {
            log.info("BexecutePost={}, object postData:{}", url, JSON.toJSONString(postData));
            responseString = executeMethod(postRequest, encoding);
        } catch (Exception e) {
            log.error("postData:{},{}", postData, e);
            throw e;
        } finally {
            postRequest.releaseConnection();
        }
        return responseString;
    }
 public static String doJsonRequest(String url, String postData, Map header, String encoding) throws Exception {
        log.info("请求URL:{}", url);

        //头部请求信息 
        Header[] headers = initHeader(header);

        PostMethod postRequest = new PostMethod(url.trim());

        if (headers != null) {
            for (int i = 0; i < headers.length; i++) {
                postRequest.setRequestHeader(headers[i]);
            }
        }

        RequestEntity se = new StringRequestEntity(postData, "application/json", encoding);
        
        postRequest.setRequestEntity(se);

        return executeMethod(postRequest, encoding);
    }


 

  1. 接口入参只有一个基础类型时:入参直接传值,不用其他处理
    消费者:
        public Response<ProductInfoDto> queryProductInfoByCode(String code) {
            return doRequestJson(code, "http://noob.test.za.net/productInfoApi/queryProductInfoByCode");
        }

    生产者:

     @RequestMapping(value = "queryProductInfoByCode", method = RequestMethod.POST)
        public Response<ProductInfoDto> queryProductInfoByCode(String code);
  2. 接口入参有多个基础类型时:
  3. 接口入参有一个对象类型
  4. 接口入参有多个基础类型和一个对象类型
  5. 接口入参不能有多个对象类型

猜你喜欢

转载自my.oschina.net/u/3434392/blog/1826264