I need to make a call to a service using Spring's RestTemplate using the HTTP PATCH verb. From what I read I need to use the execute() or exchange() method, but I have no idea on how to use it. The service call returns a HTTP 200 OK status, as well as a JSON object which I'm not particularly interested in.
Any help will be appreciated thanks.
It is possible to use the PATCH verb, but you must use the Apache HTTP client lib with the RestTemplate class with exchange(). The mapper portion may not be necessary for you. The EmailPatch class below only contains the field we want to update in the request.
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper);
HttpClient httpClient = HttpClients.createDefault();
RestTemplate restTemplate = new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
EmailPatch patch = new EmailPatch();
patch.setStatus(1);
ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.PATCH, new HttpEntity<EmailPatch>(patch),
String.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With