简体   繁体   中英

Why isn't REST accessing existing entries in MongoDB?

I have an example entry in MongoDB. I am using Spring following the tutorial found here: https://spring.io/guides/gs/accessing-mongodb-data-rest/

在此处输入图片说明

Everything loads fine, but for whatever reason it's not recognizing that entry in the database.

Player.class

package com.company.botmiteserver;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;

public class Player {
@Id
public ObjectId _id;

public String email;
public String account_name;
public String password;
public String pin;
public String status;
public String type;
public boolean active;

public Player () { }

public Player(ObjectId _id, String email, String account_name, String password, String pin, String status, String type, Boolean active) {
    this._id = _id;
    this.email = email;
    this.account_name = account_name;
    this.password = password;
    this.pin = pin;
    this.status = status;
    this.type = type;
    this.active = active;
}

public String get_id() { return _id.toHexString(); }
public void set_id(ObjectId _id) { this._id = _id; }

public String getEmail() {
    return email;
}

public String getAccountName() {
    return account_name;
}

public String getPassword() {
    return password;
}

public String getPin() {
    return pin;
}

public String getStatus() {
    return status;
}

public String getType() {
    return type;
}

public String setType(String type) {
    return this.type = type;
}

public Boolean getActive() {
    return active;
}

public Boolean setActive(Boolean active) {
    return this.active = active;
}

}

PlayerRepository.class

package com.company.botmiteserver;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource(collectionResourceRel = "player", path = "player")
public interface PlayerRepository extends MongoRepository<Player, String> {

    Player findBy_id(ObjectId _id);

//    public Player findByEmail(String email);
//    public List<Player> findByType(@Param("type") String account_name);


}

PlayerController.class

package com.company.botmiteserver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/player")
public class PlayerController {
    @Autowired
    private PlayerRepository repository;
}

Result:

    {
  "_embedded" : {
    "player" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/player"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/player"
    },
    "search" : {
      "href" : "http://localhost:8080/player/search"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }
}

It was so obvious. I needed to configure the spring.data.mongodb.database in the application.properties

ie spring.data.mongodb.database = foo

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