Spring RestTemplate发送列表和获取列表

oxf4rvwz  于 2022-10-30  发布在  Spring
关注(0)|答案(3)|浏览(159)

我想用Spring的RestTemplate做一个服务,在我的服务端代码如下:

@PostMapping(path="/savePersonList")
@ResponseBody
public List<Person> generatePersonList(@RequestBody List<Person> person){
    return iPersonRestService.generatePersonList(person);
}

在客户端,如果我使用以下代码调用服务:

List<Person> p = (List<Person>) restTemplate.postForObject(url, PersonList, List.class);

我不能将p对象用作List<Person>,它将变成LinkedHashList。经过一些研究,我找到了一个解决方案,即我必须使用exchange方法调用服务:

ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, personListResult, new ParameterizedTypeReference<List<Person>>() {});

并且使用此解决方案,服务器不能接受对象并引发异常,正确的方法是什么?

2ekbmq32

2ekbmq321#

检查您的代码是否如下所示。这应该可以工作。

//header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
//person list
List<Person> personList = new ArrayList<Person>();
Person person = new Person();
person.setName("UserOne");  
personList.add(person);
//httpEnitity       
HttpEntity<Object> requestEntity = new HttpEntity<Object>(personList,headers);
ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, requestEntity,new ParameterizedTypeReference<List<Person>>() {});
ujv3wf0j

ujv3wf0j2#

可能对你有帮助。

List<Integer> officialIds = null;
  //add values to officialIds 
  RestTemplate restTemplate = new RestTemplate();

  HttpHeaders headers = new HttpHeaders();

  HttpEntity<List<Integer>> request = new HttpEntity<List<Integer>>(officialIds, 
    headers);

  ResponseEntity<YourResponseClass[]> responses = 
  restTemplate.postForEntity("your URL", request , YourResponseClass[].class );

  List<YourResponseClass> list = Arrays.asList(responses.getBody());
ne5o7dgx

ne5o7dgx3#

我也遇到过类似的挑战,下面是我的2分。
具有POSTMap的我的控制器类

@PostMapping("/fetch/projects")
public ResponseEntity<List<Project>> getAllProjects(@RequestBody List<UserProject> userProject) {

    // Convert List of projectIds to List of Long using Lambda Functions
    List<Long> projectIds = userProject.stream().map(UserProject::getProjectId).collect(Collectors.toList());

    List<Project> projectList = projectService.getAllProjectsByProjectIds(projectIds);

    log.info("<< Fetching Projects based on Project Ids !");

    return new ResponseEntity<List<Project>>(projectList, HttpStatus.OK);
}

上述控制器的我的静态URL

final String projectBaseUri = "http://localhost:6013/api/v1/project/fetch/projects";

我的呼叫者服务

protected List<Project> getProjectsByIds(List<UserProject> userProject) {

    String url = projectBaseUri;
    log.info("Project URL : " + url);

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity<Object> requestEntity = new HttpEntity<Object>(userProject,headers);

    ResponseEntity<List<Project>> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity,new ParameterizedTypeReference<List<Project>>() {});

    return response.getBody();
}

相关问题