Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring RestTemplate将JSON转换为CSV API_Spring_Spring Boot_Spring Resttemplate - Fatal编程技术网

使用Spring RestTemplate将JSON转换为CSV API

使用Spring RestTemplate将JSON转换为CSV API,spring,spring-boot,spring-resttemplate,Spring,Spring Boot,Spring Resttemplate,在从我自己的API中获取JSON后,我想点击一个JSON到CSV的API。JSON到CSV API需要在POST请求中传递电子邮件和JSON。现在我可以在本地存储JSON,但是,我如何在请求中传递电子邮件和JSON,以及如何处理来自响应的CSV 控制器 @PostMapping("/generateExcel") public String getEmployeeCsv(@RequestBody String email) { HttpHeaders

在从我自己的API中获取JSON后,我想点击一个JSON到CSV的API。JSON到CSV API需要在POST请求中传递电子邮件和JSON。现在我可以在本地存储JSON,但是,我如何在请求中传递电子邮件和JSON,以及如何处理来自响应的CSV

控制器

@PostMapping("/generateExcel")
public String getEmployeeCsv(@RequestBody String email) {
        
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    
    HttpEntity<String> entity = new HttpEntity<String>(headers);
        
    String json = restTemplate.exchange("http://localhost:8080/SwaggerTest/employees", HttpMethod.GET, entity, String.class).getBody();
        
    entity = new HttpEntity<String>(json, email, headers);
        
    return restTemplate.exchange("https://json-csv.com/api/getcsv", HttpMethod.POST, entity, String.class).getBody();
}
从“您可以使用@RequestBody注释通过HttpMessageConverter将请求正文读取并反序列化为对象…”

因此,我假设您可以创建下面的对象,并将其用作端点中的参数

公共类EmployeeCsvParams{
/*田地*/
私人字符串电子邮件;
私有字符串json;
/*接球手和接球手*/
公共字符串getEmail(){返回this.email;}
public void setEmail(字符串email){this.email=email;}
公共字符串getJson(){返回this.json;}
public void setJson(字符串json){this.json=json;}
}
@PostMapping(“/generateExcel”)
公共字符串getEmployeeCsv(@RequestBody EmployeeCsvParams EmployeeCsvParams)
{
/* ... */ 
} 

谢谢@Adrien!任何关于我应该如何处理CSV的想法,因为我确实得到了200 OK的回复。
@PostMapping("/generateExcel")
    public String getEmployeeCsv(@RequestBody String email) {
        
        HttpHeaders headers  = new HttpHeaders();
        EmployeeCsvParams params = new EmployeeCsvParams();
        
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        
        String json = restTemplate.exchange("http://localhost:8080/SwaggerTest/employees", HttpMethod.GET, entity, String.class).getBody();
        
        params.setEmail(email);
        params.setJson(json);
        HttpEntity<EmployeeCsvParams> entity2 = new HttpEntity<EmployeeCsvParams>(params, headers);
        
        return restTemplate.exchange("https://json-csv.com/api/getcsv", HttpMethod.POST, entity2, String.class).getBody();
    }