异常全文: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);