'When I run with spring boot application I am getting the below error, Here I am trying to parse CSV file data into the database.'
'Failed to complete request: java.lang.IllegalArgumentException: Header name 'first_name' not found. Available columns
are: [first_name;last_name;account_type;email;address;zipcode;Location]
2022-10-14 19:29:31.510 ERROR 5256 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;
nested exception is java.lang.IllegalArgumentException: Header name 'first_name' not found. Available columns are: [first_name;last_name;account_type;email;address;zipcode;Location]] with root cause.'
My Entity class:
@Entity
@Table(name = "account_details")
public class AccountDetails {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.TABLE)
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "account_type")
private String accountType;
@Column(name = "email")
private String email;
@Column(name = "address")
private String adddress;
@Column(name = "zipcode")
private String zipcode;
@Column(name = "Location")
private String location;
public AccountDetails() {
super();
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAdddress() {
return adddress;
}
public void setAdddress(String adddress) {
this.adddress = adddress;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
My respostiry class:
package com.alavu.fileupload_in_springboot_csv_to_db.entity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AccountRepository extends JpaRepository<AccountDetails,Integer> {
}
My controller class:
@RestController
public class BankController {
@Autowired
AccountRepository service;
@PostMapping("/upload")
public String uploadData(@RequestParam("file") MultipartFile file) throws IOException {
List<AccountDetails> accountList = new ArrayList<>();
InputStream inputStream = file.getInputStream();
CsvParserSettings settings = new CsvParserSettings();
settings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(settings);
List<Record> parseAllRecords = parser.parseAllRecords(inputStream);
parseAllRecords.forEach(record->{
AccountDetails account = new AccountDetails();
// account.setAccountNumber(Integer.parseInt(record.getString("account_number")));
account.setFirstName(record.getString("first_name"));
account.setLastName(record.getString("last_name"));
account.setAccountType(record.getString("account_type"));
account.setAdddress(record.getString("address"));
account.setEmail(record.getString("email"));
account.setZipcode(record.getString("zipcode"));
account.setLocation(record.getString("Location"));
accountList.add(account);
});
service.saveAll(accountList);
return "Upload Successfull !!!";
}
}
CSV File data:
first_name;last_name;account_type;email;address;zipcode;Location
smith;12se74;rb9012;Rachel;Booker;Sales;Manchester
rahul;04ap67;lg2070;Laura;Grey;Depot;London
postman Response:
"java.lang.IllegalArgumentException: Header name 'first_name' not found. Available columns are: [first_name;last_name;account_type;email;address;zipcode;Location]\r\n\tat com.univocity.parsers.common.record.RecordMetaDataImpl.getMetaData(RecordMetaDataImpl.java:50)\r\n\tat com.univocity.parsers.common.record.RecordMetaDataImpl.metadataOf(RecordMetaDataImpl
Can you please help me to resolve this issue at earliest.???
CodePudding user response:
Do it like this:
account.setFirstName(record.getString("firstName"));
account.setLastName(record.getString("lastName"));
account.setAccountType(record.getString("accountType"));
account.setAdddress(record.getString("address"));
account.setEmail(record.getString("email"));
account.setZipcode(record.getString("zipcode"));
account.setLocation(record.getString("Location"));
accountList.add(account);