简体   繁体   English

Spring Rest 模板从数组中的请求正文中删除引号

[英]Spring Rest Template remove quotes from request body in array

I would like to send request with request body:我想发送带有请求正文的请求:

{
"params": [1, 124, "ABC"]
}

My code:我的代码:

        val headers = HttpHeaders()
        headers.contentType = MediaType.APPLICATION_JSON

        val arrayNode = JsonArray()
        arrayNode.add(1)
        arrayNode.add(124)
        arrayNode.add("ABC")
        val map: MultiValueMap<String, String> = LinkedMultiValueMap()
        map.add("params", arrayNode.joinToString())

        val request = HttpEntity<MultiValueMap<String, String>>(map, headers)

        val response = restTemplate.postForEntity(
            URL,
            request,
            String::class.java
        )
        return response.body

but its generate request body:但它的生成请求正文:

Request body: {"params":["1, 124, \"A\""]}

Is there any way to send request without quotes?有没有什么方法可以发送不带引号的请求?

In the comments I explained the reason why you see why you see quotes in your request body.在评论中,我解释了您为什么会在请求正文中看到引号的原因。
Here is the code you can use to send body without quotes:这是您可以用来发送不带引号的正文的代码:

  Map<String, List> req = new HashMap<>();
            List list = new ArrayList<>();
            list.add(1);
            list.add(124);
            list.add("A");
            req.put("params", list);
            HttpEntity<Map> request = new HttpEntity<>(req, headers);        
            ResponseEntity<String> result = restTemplate.postForEntity(uri, request, String.class);

Using this code, your request body should look like this:使用此代码,您的请求正文应如下所示:

"params": [1, 124, "ABC"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM