java - 在属性 Spring Boot/MongoDB 存储库中获取具有不同数据类型的条目
问题描述
几个星期以来,我一直在这个问题上转来转去。所以,有这个项目,我应该从 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
这是类模型Example.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>{
}
这是服务ExampleService.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();
}
}
这是控制器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"
...
根据我有根据的猜测,它应该是由模型上的 int 类型和数据库条目上的 string 类型之间的矛盾引起的。同样,我们不允许修改数据库的条目
问题
有什么方法可以使用 Spring Boot 从 MongoDB 中提取具有不同数据类型的属性的条目?
问题的另一种解释:有没有办法将 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 代码约定
推荐阅读
- python - Amazon Sagemaker 从 S3 存储桶打开 json
- javascript - 将 tiff 转换为 png 并在浏览器中显示
- python - 安装 tensorflow-gpu 破坏了 tensorflow
- android - 有什么方法可以在 android 房间数据库查询中绑定嵌入式类型的参数?
- r - 通过删除 tidyverse 中的重复条目来缩短 tibble/df
- java - 如何检测一个元素是否是数组中的最后一个元素?
- css - 将离子卡与 Angular 指令垂直对齐
- search - 有没有办法在用户停止输入后发送请求?
- kotlin - MapViewPlugin.kt: (168, 34): 只允许安全 (?.) 或非空断言 (!!.) 调用
- python - (Python) numpy genfromtxt 转换问题