在Spring中获取和查询MongoDB数据

时间:2019-03-18 04:54:37

标签: spring mongodb spring-boot spring-mvc spring-data-jpa

我是Java和Spring框架的新手,目前,我将构建从MongoDB获取数据的API服务。我有一个数据库和一个集合,当我通过Swagger调用它时,我想将其显示为JSON文件。

收藏

The database name is coba2 and the collection is Datalocationmodel

数据模型

@Document
public class DataLocationModels {
@Id
private String _id;
private String ann_content_category;
private String ann_sub_content_type;
private String ann_emotion;
private List<String> ann_hashtag;
private List<String> ann_keywords;

public DataLocationModels(String _id,String ann_content_category, String ann_sub_content_type, String ann_emotion, List<String> ann_hashtag, List<String> ann_keywords) 

{this._id = _id;this.ann_content_category = ann_content_category; this.ann_sub_content_type = ann_content_sub_content_type; this.ann_emotion = ann.emotion; this.ann_hashtag = ann_hashtag; this.ann_keywords = ann_keywords}

/.. the getter and setters

存储库

@Repository

// Inherit from MongoRepository lib
public interface DataLocationRepository extends MongoRepository<DataLocationModels, String > {

    // Get data based on id
    public DataLocationModels findBy_id(String _id);

服务

@Service
public class DataLocationService {
    @Autowired
    private DataLocationRepository locationRepository;

    // Retrieve operation
    public List<DataLocationModels> getAll() {
        return locationRepository.findAll();
    }

控制器

@RestController
@RequestMapping("/datalocation")
public class DataLocationController {
    @Autowired
    private DataLocationService repository;

    @RequestMapping(value="/", method=RequestMethod.GET)
    public List<DataLocationModels> getAllDataLocationModels() {
        return repository.getAll();
    }

我的application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=coba2

当我张开招呼并尝试打电话给所有人获取所有数据时,为什么我什么都没得到?

0 个答案:

没有答案