1
"location"のようなクラスがあります。 「場所」クラスには4つのフィールドがあります。 フィールドは:id、city、state、country .. 国は2つのフィールド、国コード、国名を含むseprateクラスです.2フィールドはロケーションクラスから読み込まなければなりません。 "locationMongoRepository.save()それは結合ミスマッチとしてエラーを示します。 mongodbに保存する方法を解決してください。私はjson値を持っています。mongodbにspring mvcを使用して保存する方法
public void insertLocation() throws InvalidFormatException, IOException, JSONException{
FileInputStream inp;
Workbook workbook;
try {
inp = new FileInputStream("/home/Downloads/eclipse/Workspace/Samplboot-master latest/cityListForIndia1.xlsx");
workbook = WorkbookFactory.create(inp);
Sheet sheet = workbook.getSheetAt(0);
JSONArray json = new JSONArray();
boolean isFirstRow = true;
ArrayList<String> rowName = new ArrayList<String>();
for (Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext();)
{
Row row = rowsIT.next();
JSONObject jRow = new JSONObject();
if(isFirstRow)
{
for (Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext();)
{
Cell cell = cellsIT.next();
rowName.add(cell.getStringCellValue());
}
isFirstRow = false;
}
else
{
JSONObject jRowCountry= new JSONObject();
JSONObject jRowLocation= new JSONObject();
jRowLocation.put("city", row.getCell(0));
jRowLocation.put("state", row.getCell(1));
jRowCountry.put("country",row.getCell(2));
jRowCountry.put("countryCode", row.getCell(3));
jRowLocation.put("country", jRowCountry);
System.out.println("Location"+jRowLocation.toString());
}
}
}
catch (InvalidFormatException e) {
// TODO Auto-generated catch block
System.out.println("Invalid Format, Only Excel files are supported");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Check if the input file exists and the path is correct");
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("Unable to generate Json");
e.printStackTrace();
}
}
}