繁体   English   中英

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

[英]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.

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