我必须使用Resttemplate调用PUT方法。我可以通过POST Man访问该服务。但是当我尝试使用Resttemplate从Java尝试相同的请求时,它会抛出错误。我在做什么可能是错误的。
405 : [{"category":"ACCESS","code":"METHOD_NOT_SUPPORTED","description":"Request method 'PUT' not
supported","httpStatusCode":"405"}]
@Autowired
@Qualifier("orderMasterUpdateClient")
private RestTemplate orderMasterUpdateClient; // Loading the template with credentials and URL
ResponseEntity<SalesOrderDocument> responseEntity = orderMasterUpdateClient.exchange(
URL,
HttpMethod.PUT,
new HttpEntity<>(headers),
SalesOrderDocument.class, changeRequest);
最佳答案
如果要在PUT请求的正文中发送changeRequest
对象数据,建议您使用下一个RestTemplate交换方法调用:
String url = "http://host/service";
ChangeRequest changeRequest = new ChangeRequest();
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity<ChangeRequest> httpEntity = new HttpEntity<>(changeRequest, httpHeaders);
ResponseEntity<ChangeRequest> response = restTemplate
.exchange(url, HttpMethod.PUT, httpEntity, ChangeRequest.class);