简体   繁体   中英

What is the proper way to connect spring boot with mongo and read document?

Before all, I'm testing in Postman with this URL: http://localhost:8080/skiing/getSkiing , response is:

[
{}
]

I don't know is this all that I need for Mongo configuration with Spring, if it's no, can someone link me an example with good way how to connect Spring Boot with Mongo. And also, if this is all what I need for mongo configuration, how Spring read this? Where is this called or where Spring Boot actually use this?

spring.data.mongodb.database=tripadvisor
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.servlet.multipart.max-file-size=256MB
spring.servlet.multipart.max-request-size=256MB
spring.servlet.multipart.enabled=true

Anyway, my response after trying to read all elements from documents is empty. This is my code for that:

Repo :

@Repository
public interface SkiingRepository extends MongoRepository<Skiing, String> {
}

Service

@Service
public class SkiingServiceImpl implements SkiingService {

    @Autowired
    private SkiingRepository skiingRepository;

    @Override
    public List<Skiing> getAllSkiing() {
        return skiingRepository.findAll();
    }    
}

Controller :

@RestController
@RequestMapping("/skiing")
public class SkiingController {

    @Autowired
    SkiingService skiingService;

    @GetMapping(value = "/getSkiing")
    public ResponseEntity<?> getAllSkiing() {
        List<Skiing> skiingList = skiingService.getAllSkiing();
        return new ResponseEntity<Object>(skiingList, HttpStatus.OK);
    }
}

There are some basic configurations when you work with Spring and MongoDB:

spring.data.mongodb.uri=
spring.data.mongodb.database=
spring.data.mongodb.username=
spring.data.mongodb.password=
spring.data.mongodb.port=
spring.data.mongodb.host=

The answer for question "Where is this called or where Spring Boot actually use this?"is Spring Data MongoDB - a project of Spring Framework

Please refer Spring Data MongoDB for more information

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