I am using Spring MVC for a Rest API and am trying to call the following method on the controller:
@RequestMapping(method=RequestMethod.POST, value=IApiVersion.VERSION_ALL_PREFIX + ORG_RESPONSE_PAUSE_WRITES_URL)
public @ResponseBody Boolean setResponsePauseWrites(@RequestParam List<Long> orgIds, @RequestParam boolean pauseWrites){
validateDatabusClient();
organizationService.setPauseResponseWrites(orgIds, pauseWrites);
return Boolean.TRUE;
}
I am using Spring's RestTemplate
to submit the request like this:
@Override
public void setPauseWrites(List<Long> orgIds, boolean pauseWrites){
String orgIdString = StringUtils.collectionToCommaDelimitedString(orgIds);
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("orgIds", orgIdString);
parameters.add("pauseWrites", Boolean.toString(pauseWrites));
restTemplate.postForObject(orgResponsePauseWritesURL, parameters, Boolean.class);
}
This works fine and all, but I would prefer to not need to convert the list of orgIds to a comma delimited string. I am frustrated because the spring mvc controller has no problem converting the strings back to the parameters it is expecting, List and boolean. I would expect the RestTemplate to have a built in message converter to handle basic java classes like List and Boolean.
When I try this code:
@Override
public void setPauseWrites(List<Long> orgIds, boolean pauseWrites){
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
parameters.add("orgIds", orgIds);
parameters.add("pauseWrites", Boolean.valueOf(pauseWrites));
restTemplate.postForObject(orgResponsePauseWritesURL, parameters, Boolean.class);
}
I get the following exception message:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.util.ArrayList]
What is my best option? I am planning on continuing to convert the parameters to Strings for the rest template call for now, but I would like to know if there is a MessageConverter I can add to my RestTemplate to make this work. Currently I am just using the default MessageConverters. I have tried adding the MappingJacksonMessageConverter
and changing my content type header to support json, but I get the same results.
Thank you.