Search code examples
javaspringrestspring-rest

Spring Resttemplate exchange method to call a PATCH request


I have a Java 8 application with Spring framework. We need to call a patch operation using REST from our application.

The code snippet is as follows:

import com.xyz.myobject.ResponseAdmission;
import com.xyz.myobject.RequestAdmission;

@Repository
public class AdmissionRepository {

   @Autowired
   private RestTemplate restTemplate;
   
   public ResponseObject updateAdmission(RequestAdmission requestAdmission, String admissionId) {

   UriBuilder uriBuilder =  UriBuilder.fromUri("https://admissionportal.com").path("admission").path("admissionId="+admissionId);

   HttpEntity<RequestAdmission> admissionEntity = new HttpEntity<>(requestAdmission, this.getHeaders);
   
   ResponseEntity<ResponseAdmission> responseEntity = this.restServiceESBSupport.exchange(uriBuilder.build(), HttpMethod.PATCH, admissionEntity , ResponseAdmission.class);
 
   return responseEntity.getBody();

}

public MultiValueMap<String, String> getHeaders() {
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Accept", "application/xml");
        headers.add("Content-Type", "application/xml");
        return headers;
    }

}

When I execute the above code, when the line with 'exchange' keyword is executed, it gives an error as:

org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [com.xyz.myobject.RequestAdmission] and content type [application/xml]

When I debugged the code, the restTemplate has the following 7 convertors registered:

ByteArrayHttpMessageConverter,
StringHttpMessageConverter,
ResourceHttpMessageConverter,
SourceHttpMessageConverter,
AllEncompassingFormHttpMessageConverter,
Jaxb2RootElementHttpMessageConverter,
MappingJackson2HttpMessageConverter

I also tried to add following line:

this.restTemplate.getMessageConverters().add(new Jaxb2RootElementHttpMessageConverter());

But still not working.

RequestAdmission and ResponseAdmission are the JAXB POJO objects generated from xsds.

The webservice accepts the request in form of XML and response is also in form of XML.


Solution

  • You can create your own XML as a string and use it your request, like so:

    @Repository
    public class AdmissionRepository {
    
       @Autowired
       private RestTemplate restTemplate;
    
       public ResponseObject updateAdmission(RequestAdmission requestAdmission, String admissionId) {
    
       UriBuilder uriBuilder =  UriBuilder.fromUri("https://admissionportal.com").path("admission").path("admissionId="+admissionId);
       String xmlString = // create an actual valid XML string with all the <tag>value</tag> and the whole structure.
       HttpEntity<String> admissionEntity = new HttpEntity<>(xmlString, this.getHeaders);
    
       ResponseEntity<ResponseAdmission> responseEntity = this.restServiceESBSupport.exchange(uriBuilder.build(), HttpMethod.PATCH, admissionEntity , ResponseAdmission.class);
    
       return responseEntity.getBody();
    
    }