您的问题存在多个问题
- ''是java中的字符,""是String
- 没有提到请求类型GET/POST,从
postForObject推断
- 如果您使用 POST 调用,那么您可以将其作为 BodyParam 传递给提及 here,而不是作为 RequestParam 传递,但 RequestParam 仍然可以与 POST 调用一起使用。
解决方案 1:考虑使用 @RequestBody 进行 POST 调用
控制器:
@PostMapping(value = "/so/2987755", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<List<String>> functionName(@RequestBody List<String> a){
for(int i = 0; i < a.size(); i++){
System.out.println(a.get(i));
}
return new ResponseEntity<>(a, HttpStatus.OK);
}
客户:
List<String> a = Collections.singletonList("b");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
final HttpEntity<String> request = new HttpEntity<String>(objectMapper.writeValueAsString(a), headers);
return restTemplate.postForObject("http://localhost:8080/so/2987755", request,String.class);
解决方案 2:使用 @RequestParam 进行 POST 调用
控制器:
@PostMapping(value = "/so/2987755", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<List<String>> functionName(@RequestParam(value = "a") List<String> a){
for(int i = 0; i < a.size(); i++){
System.out.println(a.get(i));
}
return new ResponseEntity<>(a, HttpStatus.OK);
}
客户端 1:
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
Map<String, String> requestParam = new HashMap<>();
final String requestParamKey = "a";
requestParam.put(requestParamKey, "b,c,d");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
final HttpEntity<String> request = new HttpEntity<String>(headers);
return restTemplate.postForObject("http://localhost:8080/so/2987755?a={" + requestParamKey + "}", request, String.class, requestParam);
客户端 2:使用 UriComponentsBuilder 的另一种客户端风格
MultiValueMap<String, String> requestParam = new LinkedMultiValueMap<>();
final String requestParamKey = "a";
List<String> a = Arrays.asList("b", "c", "d");
requestParam.put(requestParamKey, a);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/so/2987755");
builder.queryParams(requestParam);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(headers);
return restTemplate.postForObject(builder.build().encode().toUri(), request, String.class);
这两个客户端调用都在控制器上打印
b
c
d
RequestParam 打印为 ["b"] 的原因是因为您在 LinkedValueMap 中使用了 List<String>,而不是如果您仅使用 MultiValueMap<String, String>,事情将按您的预期工作。
您观察到的是,在反序列化 List 时,它将变为 ["b"],但如果您使用 UriComponentsBuilder,您将不会遇到此类问题。