不使用morphia和mongoDB中的类返回超级类元素

时间:2014-09-28 19:15:54

标签: mongodb jersey morphia

我有基类

@XmlRootElement
public abstract class BaseDO {

    @Id
    protected ObjectId id;

    /**
     * We'll only provide getters for these attributes, setting is done in @PrePersist.
     */
    protected Date creationDate;
    protected Date lastChange;

.....和用户类:

@Entity(value = "user", noClassnameStored = true)
@XmlRootElement(name = "user")
@XmlSeeAlso({BaseDO.class})
public class AtsUser extends BaseDO {

    public static enum UserStatus {
        CREATED, ACTIVE, INACTIVE, DELETED, CLOSED
    }

    @Indexed(unique = false)
    private String firstName;
    @Indexed(unique = false)
    private String lastName;
    @Indexed(unique = false)
    private String email;
    private String password;

    @Embedded
    private List<UserRoleDO> roles = new ArrayList<UserRoleDO>();

    // private String userId; //TODO add this later
    private UserStatus status;

    private String success;

.....

REST API如下:

@Path("user/validate")
public class AtsUserValidationService {

    private AtsUserDao dao; 
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");



    @GET @Path("/query")
    @Produces(MediaType.APPLICATION_JSON)
    public AtsUser  getUserByEmailn(@QueryParam("email") String email) {

        System.out.println("in getUserByEmailn");
            dao = new AtsUserMongodbDao();
            System.out.println("firstName " + email);
            AtsUser atsUser = dao.getAtsUsersByEmail(email) ;

                return atsUser ;
    }

...

吗啡Dao如下:

@Override
public AtsUser getAtsUsersByEmail(String email) {
    AtsUser atsUser = null;
    if ((email == null) || email.isEmpty()   ) {
        return null;
    }
    System.out.println("getAtsUsersByEmail:" + email);
    Query<AtsUser> query = mongoDatastore.find(AtsUser.class);
    query.field("email").equal(email);
    atsUser = query.get();
    return atsUser;
}

.....

当我调试时,我在java代码中看到了id字段和creationDate字段,但是JSON不包含它。这是我的JSON的样子。

{

    "id": null,
    "code": "admin",
    "desc": "admin",
    "email": "admin@aa.com",
    "firstName": "admin",
    "lastName": "admin",
    "password": "admin",
    "status": "CREATED"

}

为什么我的id为null,如何从基类中获取元素以显示在JSON中?

1 个答案:

答案 0 :(得分:0)

我相信你也需要用@Entity来注释基类。它对我有用(使用morphia 0.109)。