Rest Template : I/O error on PATCH request for “http://localhost:8080/api/xxx". Invalid HTTP method: PATCH

Chiwa Kantawong (Pea)
1 min readAug 9, 2019

--

String body = "{\"data\" : \"some json\"}";
HttpHeaders headers = new HttpHeaders();headers.set(CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())
HttpEntity<String> request = new HttpEntity<>(body, headers);
RestTemplate restTemplate =
new RestTemplate(new HttpComponentsClientHttpRequestFactory());
String url = “http://localhost:8080/api/xxx";String response = restTemplate.patchForObject(url, request, String.class);

พระเอกของเราคือ HttpComponentsClientHttpRequestFactory นั่นเองครับ

ถ้าไม่มีตัวนี้เราจะได้ error ประมาณนี้ครับ

08:48:52.717 ERROR c.n.d.t.s.PersonServiceImpl - Unexpected Exception  : 
org.springframework.web.client.ResourceAccessException: I/O error on PATCH request for "http://localhost:8080/api/xxx":Invalid HTTP method: PATCH; nested exception is java.net.ProtocolException: Invalid HTTP method: PATCH
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:580) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:545) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:466) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at ..........

--

--

Chiwa Kantawong (Pea)
Chiwa Kantawong (Pea)

Written by Chiwa Kantawong (Pea)

Software Development Expert at Central Tech

No responses yet