How to use Spring`s RestTemplate to POST an array of strings

ぃ、小莉子 提交于 2019-12-12 16:06:46

问题


I am trying to post a simple array of strings using the spring`s restTemplate. Did anyone succeed with that ?

The client:

    public void save(){
        String company = "12345";
        String productId = "10";
        String[] colors = {"A","B","C","D","E"};
        String convertUrl = "http://localhost:8080/cool-web/save";
        MultiValueMap<String, Object> convertVars = new LinkedMultiValueMap<String, Object>();
        convertVars.add("companyID", StringUtils.trimToEmpty(company));
        convertVars.add("productId", StringUtils.trimToEmpty(productId));
        convertVars.add("disclaimer", StringUtils.trimToEmpty("ffs"));
        convertVars.add("colorsArray", colors); 
        restTemplate.postForObject(convertUrl, null, String.class, convertVars);
}

The Service is:

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseStatus(value = HttpStatus.OK)
    public void save(@RequestParam("colorsArray[]") String[] colors,
        @RequestParam("disclaimer") String disclaimer,
        @RequestParam("companyID") String companyID,
        @RequestParam("productId") String productId) {

    resourceService.save(colors, disclaimer, companyID, productId);
}

I got 400 Bad Request.

What am I doing wrong ?

I am using the default messageConverters.

Do I need to implement custom messageConverter for a simple array of Strings ?


回答1:


Here is the solution:

public void save(){

    String company = "12345";
    String productId = "10";
    String[] colors = {"A","B","C","D","E"};
    String convertUrl = "http://localhost:8080/cool-web/save";

    MultiValueMap<String, Object> convertVars = new LinkedMultiValueMap<String, Object>();

    convertVars.add("companyID", StringUtils.trimToEmpty(company));
    convertVars.add("productId", StringUtils.trimToEmpty(productId));
    convertVars.add("disclaimer", StringUtils.trimToEmpty("ffs"));

    for(String color:colors){
        convertVars.add("colorsArray[]", color); 
    }

    restTemplate.postForObject(convertUrl, convertVars , String.class); 
}



回答2:


If you are trying POST MultiValueMap, use Map.class instead of String.class in the code:

restTemplate.postForObject(convertUrl, null, Map.class, convertVars);

And your service method is wrong I guess. Because you are posting MultiValueMap and in save method you are trying to get all the internal variables as method parameters i.e. RequestParam. That's not going to happen. You will have to accept only MultiValueMap there and take things out of it for use.

public void save(@RequestParam("colorsArray[]") MultiValueMap<String, Object> convertVars ) {
    resourceService.save(convertVars.getColors(), .... );
}  



回答3:


@RequestParam(value) value - "The name of the request parameter to bind to." So @RequestParam("colorsArray[]") String[] colors trying to find param with name "colorsArray[]" but u put parameter with name "colorsArray". May be that is the reason.



来源:https://stackoverflow.com/questions/27643589/how-to-use-springs-resttemplate-to-post-an-array-of-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!