異常全文:java.net.ProtocolException: cannot write to a URLConnection if doOutput=false - call setDoOutput(true)
異常分析:
1、發起GET請求,請求的HttpEntity中包含body參數。
解決方案:改變聲明HttpEntity的方式——
httpEntity = new HttpEntity<>(restUtilParams.getBody(), httpHeaders);
==> HttpEntity httpEntity = new HttpEntity<>(httpHeaders);
ResponseEntity<String> responseEntity =
templateMap.get(TEMPLATE).exchange(uri, HttpMethod.GET, httpEntity, String.class);
2、URLConnection發起POST請求少設置參數
解決方案:為URLConnection 添加兩參數——
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);