【问题标题】:Get entries with Different Data Types in a Property Spring Boot/MongoDB Repository在属性 Spring Boot/MongoDB 存储库中获取具有不同数据类型的条目
【发布时间】:2021-07-31 23:52:15
【问题描述】:

我已经在这个问题上反复讨论了几个星期。所以,有这个项目,我应该从 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类型之间的矛盾造成的。同样,我们不允许修改数据库的条目

问题
有什么方法可以从 MongoDB 中提取具有 Spring Boot 属性的不同数据类型的条目?

问题的另一种解释:有没有办法将 MongoDB 中的数据库条目提取为原始 JSON?

谢谢你:)

【问题讨论】:

    标签: java spring mongodb spring-boot spring-mvc


    【解决方案1】:

    您可以将数据类型从 Integer 替换为 String 并添加新方法以将列表 Integer 获取到您的实体(将 String 转换为 Integer):

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

    请阅读 java 代码约定

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-18
      • 2019-12-02
      • 2023-01-25
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 2015-07-10
      • 2018-03-30
      相关资源
      最近更新 更多