简体   繁体   English

在属性 Spring 引导/MongoDB 存储库中获取具有不同数据类型的条目

[英]Get entries with Different Data Types in a Property Spring Boot/MongoDB Repository

I've been going round and round on this problem for a couple of weeks.几个星期以来,我一直在这个问题上转来转去。 So, there's this project where I should get all entries from a SpringBoot Backend (RestAPI) from MongoDB repository.所以,有这个项目,我应该从 MongoDB 存储库中获取 SpringBoot 后端(RestAPI)的所有条目。

How my Backend looks like我的后端是什么样子的
My lecturers have given us a database in MongoDB to work with.我的讲师给了我们一个 MongoDB 中的数据库供我们使用。 We are not allowed to change anything .我们不允许改变任何东西 Only reading from it is allowed.只允许从中读取。 To simplify the problem, let's just say there are two columns.为了简化问题,假设有两列。 On one column, there are multiple entries with multiple data types.在一列上,有多个具有多种数据类型的条目。

This is how the table would look like in MongoDB Compass:这是 MongoDB Compass 中表格的样子:

_id (ObjectID) | Number (Mixed) 
60ab1          | 104              // int
60ab2          | 103              // int 
60ab3          | "102,3"          // string, this is where the problem begins

Here's the class model Example.java这是 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
}

Here's the Repository ExampleRepository.java这是存储库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>{
}

Here's the Service ExampleService.java这是服务示例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();
    }
}

And here's the Controller ExampleController.java这是 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();
    }
}

The Problem问题
The build and the connection to MongoDB was ok, but when I tried to test the API in localhost:8080/api/all, there is an error in the terminal:构建和与 MongoDB 的连接正常,但是当我尝试在 localhost:8080/api/all 中测试 API 时,终端出现错误:

java.lang.NumberFormatException: For input string: "102,3"
...

Based on my educated guess, it should be caused by a contradiction between the int type on the model and the string type on the database entry.根据我有根据的猜测,这应该是由 model 上的 int 类型与数据库条目上的 string 类型之间的矛盾引起的。 Again, we are not allowed to modify the entries of database同样,我们不允许修改数据库的条目

Question问题
Are there any way that I could extract entries from MongoDB with different data types on a property with Spring Boot?有什么方法可以从 MongoDB 中提取具有 Spring Boot 属性的不同数据类型的条目?

Another Paraphrase of the question: are there any way to extract the database entries in MongoDB as raw JSON?问题的另一个解释:有什么方法可以将 MongoDB 中的数据库条目提取为原始 JSON?

Thank you:)谢谢:)

you can replace the data type from Integer to String and add new method to get list Integer to your entity (convert String to Integer):您可以将数据类型从 Integer 替换为 String 并添加新方法以获取列表 Integer 到您的实体(将 String 转换为 Integer):

public List<Interger> getListNumber(){
    return Arrays.stream(this.Number.split(",")).map(number -> Integer.valueOf(number)).collect(Collectors.toList());
}

And please reading java code conventions请阅读 java 代码约定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM