读取CSV分段输入时,Spring Boot分段应用失败

时间:2019-06-04 15:30:00

标签: csv spring-boot jackson

我有一个Spring Boot应用程序,希望将CSV文件与请求正文一起输入。我的应用程序失败,出现以下错误。

JSON parse error: Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('\"' (code 34)): was expecting comma to separate Object entries\n at [Source: (PushbackInputStream); line: 3, column: 2]

我的控制器

@RestController
public class DataValidatorController {

    private static final String VALIDATE_DATA_URI ="/validatedata";

    private static final Logger logger = LoggerFactory.getLogger(DataValidatorController.class);

    private DataValidateService dataValidateService;



    public DataValidatorController(DataValidateService dataValidateService) {
        this.dataValidateService = dataValidateService;
    }



    @PostMapping(VALIDATE_DATA_URI)
    public DataValidateResponse validateData(@RequestPart("metadata") DataValidateRequest dataValidateRequest,
                                             @RequestPart("datafile") MultipartFile dataFile) throws IOException {

        logger.info("validateData() Starts");
        return dataValidateService.validateData(dataValidateRequest,dataFile);
    }

}

我的CSV文件内容

姓名,年龄

person1,32

person2,33

1 个答案:

答案 0 :(得分:0)

已经使用CURL测试了此代码,我得到了200。

DataValidatorController.class

    private static final String VALIDATE_DATA_URI ="/validatedata";

    @PostMapping(value = VALIDATE_DATA_URI)
    public ResponseEntity validateData(@RequestPart("metadata") MyPojo myPojo,
                                       @RequestPart("datafile") MultipartFile dataFile) throws IOException {
        System.out.println(String.format("Data - %s", myPojo.getId() ));
        return ResponseEntity.ok().build();
    }

MyPojo.class

//Used lombok

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class MyPojo
{
    private String id;

}

CuRL请求

  

curl -i -H“内容类型:多部分/表单数据” -F“元数据=@test.json; type = application / json” -F“ datafile=@test.csv; type = application / csv “ http://localhost:8080/validatedata

test.json

{
 "id": "1"
}

test.csv

name,age
person1,32
person2,33

如果知道答案,请告诉我。

编辑

enter image description here

我在邮递员中也取得了适当的成绩。

Header => Content-Type:multipart / form-data