使用feign封装第三方Http接口,请求出现400

调用第三方接口,传输二字节流,每次批量传输都有一部分传输失败,接口响应超时,返回400,不管修改超时时间多久都不成功

一.使用feign方式

接口调用使用feign包装第三方接口

@FeignClient(name = "xxx-service", url = "${url}", configuration = { FeignClientConfig.class })
public interface ServiceFeign {

@RequestMapping(value = "${a.url}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public AResponse getA(ARequest request);

}

调用位置

复制代码
public class TestA{

  @Autoware

  private ServiceFeign serviceFeign;

  public xx methodA(xxx){

     serviceFeign.getA(xxx);

  }

}
复制代码

 

开启feign日志发现报400,第三方无响应返回

怀疑时feign的问题故换成http的:

复制代码
/**
     * 以HTTP post方式访问指定URL
     * @param url 访问URL
     * @param jsonObject json对象
     * @return 服务器响应内容
     */
    public static String jsonPost(String url, String jsonObject) {
        PostMethod method = new PostMethod(url);
        String body = null;
        if (StringUtils.isNotBlank(jsonObject)) {
            try {
                method.addRequestHeader("Content-type", "application/json; charset=utf-8");
                method.addRequestHeader("Accept", "text/xml,text/javascript,text/html,application/json");
                RequestEntity requestEntity = new StringRequestEntity(jsonObject, "application/json", "utf-8");
                method.setRequestEntity(requestEntity);
                httpClient.executeMethod(method);
                InputStream inputStream = method.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                StringBuffer stringBuffer = new StringBuffer();
                String str = "";
                while ((str = br.readLine()) != null) {
                    stringBuffer.append(str);
                }
                body = stringBuffer.toString();
                log.info("http返回结果{}",body);
            } catch (Exception e) {
                log.error("连接异常:"+e.getMessage(),e);
            } finally {
                method.releaseConnection();
            }
        }
        return body;
    }
复制代码

发现还是一样的问题(fegin底层可能也是这种方式实现的),

最后解决方法:使用map方式成功了

复制代码
/**
     * 以HTTP post方式访问指定URL
     * @param url 访问URL
     * @param paramData map对象
     * @return 服务器响应内容
     */
    @SuppressWarnings("deprecation")
    public static String jsonPost(String url, Map<String, Object> paramData) {
        PostMethod method = new PostMethod(url);
        String body = null;
        if (method != null && paramData != null) {
            try {
                method.addRequestHeader("Content-type", "application/json; charset=utf-8");
                method.addRequestHeader("Accept", "application/json");
                String jsonObject = JSONObject.toJSONString(paramData);
                method.setRequestBody(jsonObject);
                httpClient.executeMethod(method);
                log.info("http-jsonPost-jsonObject:" + method.getResponseBodyAsString());
                InputStream inputStream = method.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                StringBuffer stringBuffer = new StringBuffer();
                String str = "";
                while ((str = br.readLine()) != null) {
                    stringBuffer.append(str);
                }
                body = stringBuffer.toString();
            }catch(SocketTimeoutException te){ 
                log.error("连接超时:"+te.getMessage());
            }catch (Exception e) {
                log.error("连接异常:"+e.getMessage(),e);
            } finally {
                method.releaseConnection();
            }
        }
        return body;
    }
复制代码

记录一下,具体原因不是很明白!!!

posted on   qqq9527  阅读(4002)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示