我想用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
。
经过一些研究后,我找到了一个解决方案,说我必须用交换方法调用服务:
ResponseEntity<List<Person>> rateResponse = restTemplate.exchange(url, HttpMethod.POST, personListResult, new ParameterizedTypeReference<List<Person>>() {});
使用此解决方案,服务器无法获取对象并引发异常,这是正确的方法吗?
答案 0 :(得分:11)
检查您的代码是否如下所示。这应该有用。
//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>>() {});
答案 1 :(得分:0)
这可能对您有帮助。
function duplicateProtectSheet(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Source').activate();
var sh1=ss.duplicateActiveSheet();
var r1=sh1.getRange('B6:F12');
var r2=sh1.getRange('C3');
var r3=sh1.getRange('K13');
sh1.protect().setUnprotectedRanges([r1,r2,r3]);
}
答案 2 :(得分:0)
我遇到了类似的挑战,这是我的 2 美分。
带有 POST 映射的控制器类
@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();
}