[英]Spring Data Mongodb - repository for collection with different types
[英]Get entries with Different Data Types in a Property Spring Boot/MongoDB Repository
幾個星期以來,我一直在這個問題上轉來轉去。 所以,有這個項目,我應該從 MongoDB 存儲庫中獲取 SpringBoot 后端(RestAPI)的所有條目。
我的后端是什么樣子的
我的講師給了我們一個 MongoDB 中的數據庫供我們使用。 我們不允許改變任何東西。 只允許從中讀取。 為了簡化問題,假設有兩列。 在一列上,有多個具有多種數據類型的條目。
這是 MongoDB Compass 中表格的樣子:
_id (ObjectID) | Number (Mixed)
60ab1 | 104 // int
60ab2 | 103 // int
60ab3 | "102,3" // string, this is where the problem begins
這是 class model 示例。java
package com.project.api.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@Document
public class Example {
@Id
private String _id;
private Integer Number; //This is where the problem begins
}
這是存儲庫ExampleRepository.java
package com.project.api.repository;
import com.project.api.model.Example;
import org.json.JSONObject;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface ExampleRepository extends MongoRepository<Example,String>{
}
這是服務示例Service.java
package com.project.api.service;
import ch.qos.logback.core.encoder.EchoEncoder;
import com.project.api.model.Example;
import com.project.api.repository.ExampleRepository;
import com.project.api.exception.EntityNotFoundException;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.json.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.query.UntypedExampleMatcher;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.MongoTemplate;
import lombok.RequiredArgsConstructor;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
@Service
@RequiredArgsConstructor
public class ExampleService {
@Autowired
private final ExampleRepository exampleRepository;
private int i;
String myString;
public List<Example> getAllExample()
{
return exampleRepository.findAll();
}
}
這是 Controller ExampleController.java
package com.project.api.controller;
import com.project.api.model.Example;
import com.project.api.service.ExampleService;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import lombok.RequiredArgsConstructor;
@RestController
@RequestMapping(value = "/api")
@RequiredArgsConstructor
public class ExampleController {
@Autowired
private final ExampleService exampleService;
@GetMapping("/all")
public List<Example> getAllExample(@RequestParam(required = false) String ORT)
{
return exampleService.getAllExample();
}
}
問題
構建和與 MongoDB 的連接正常,但是當我嘗試在 localhost:8080/api/all 中測試 API 時,終端出現錯誤:
java.lang.NumberFormatException: For input string: "102,3"
...
根據我有根據的猜測,這應該是由 model 上的 int 類型與數據庫條目上的 string 類型之間的矛盾引起的。 同樣,我們不允許修改數據庫的條目
問題
有什么方法可以從 MongoDB 中提取具有 Spring Boot 屬性的不同數據類型的條目?
問題的另一個解釋:有什么方法可以將 MongoDB 中的數據庫條目提取為原始 JSON?
謝謝:)
您可以將數據類型從 Integer 替換為 String 並添加新方法以獲取列表 Integer 到您的實體(將 String 轉換為 Integer):
public List<Interger> getListNumber(){
return Arrays.stream(this.Number.split(",")).map(number -> Integer.valueOf(number)).collect(Collectors.toList());
}
請閱讀 java 代碼約定
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.