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.

有帮助吗?

解决方案

A message converter does not just deal with the Java type (such as Boolean) that is of interest to a Java programmer. Its job is to covert Java objects to and from a byte stream representation that has a format that is identified by a media type name. Spring is only ever going to provide media converters for widely used media types. There is no widely used media type for a list of arbitrary Java objects; how could there be? As for a list of integers; I doubt there will ever be such a type, ironically, because it is just a bit too simple. The TSV (tab separated values) media type is a simple type that can represent a simple list of integers.

So, yes, you will have to write your own converter.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top