简体   繁体   中英

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.

How my Backend looks like
My lecturers have given us a database in MongoDB to work with. 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:

_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

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

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

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

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:

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. 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?

Another Paraphrase of the question: are there any way to extract the database entries in MongoDB as raw 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):

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

And please reading java code conventions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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