Can't find a codec for class , Morphia , Spring

这一生的挚爱 提交于 2019-12-24 09:16:46

问题


Posting JSON towards tomcat running Spring BOOT.

Mapping requests to update Mongo via Morphia

Getting Error : "status":500,"error":"Internal Server Error","exception":"org.bson.codecs.configuration.CodecConfigurationException","message":"Can't find a codec for class se.preffo.model.ProfileAccess.

Somehow it is not knowing how to create the Object ProfileAccess

ProfileClass
package se.preffo.model;

    import java.util.List;
    import org.bson.types.ObjectId;
    import org.mongodb.morphia.annotations.*;

    @Entity("profiles")
    public class Profile extends BaseEntity {

                @Property("user_name")
                private String userName;

                @Property("profile_data")
               private List<ProfileData> profileData;

                @Property("tags")
                private List<String> profileTags;

                @Property("profile_name")
                private String profileName;

                @Embedded
                @Property("access")
                private List<ProfileAccess> profileAccess;


                public Profile (){

                    this.userName = "";
                }

                public String getUserName(){
                    return this.userName;
                }

                public void setUserName(String userId){
                    this.userName = userId;
                }

                public List<ProfileData> getProfileData(){
                    return this.profileData;
                } 

                public void setProfileData(List<ProfileData> profileData){
                    this.profileData = profileData;   
                }

                public String getProfileName() {
                    return profileName;
                }

                public void setProfileName(String profileName) {
                    this.profileName = profileName;
                }

                public List<ProfileAccess> getProfileAccess() {
                    return profileAccess;
                }

                public void setProfileAccess(List<ProfileAccess> profileAccess) {
                    this.profileAccess = profileAccess;
                }


                public List<String> getProfileTags() {
                    return profileTags;
                }

                public void setProfileTags(List<String> profileTags) {
                    this.profileTags = profileTags;
                }

                public void addTag(String tag){
                    this.profileTags.add(tag);
                }

                public void removeTag(String tag){
                    this.profileTags.remove(tag);
                }

                @Override
                public String toString() {
                    return "Profile{" +
                            "id=" + id +
                            ", userName='" + userName + '\'' +
                            ", profileData=" + profileData +
                            ", profileTags=" + profileTags +
                            ", profileName='" + profileName + '\'' +
                            ", profileAccess=" + profileAccess +
                            '}';
                }
    }    

ProfileAccess class:

           package se.preffo.model;

            import org.mongodb.morphia.annotations.Entity;
            import org.mongodb.morphia.annotations.Property;


            @Embedded
            public class ProfileAccess{

                @Property("name")
                private String accessName;

                @Property("access_id")
                private String accessId;

                @Property("exp")
                private String expiryTime;

                @Property("type")
                private String accessType;


                //Constructor
                public ProfileAccess() {
                    super();
                }

                @Override
                public String toString() {
                    return "ProfileAccess{" +
                            "accessName='" + accessName + '\'' +
                            ", accessId='" + accessId + '\'' +
                            ", expiryTime='" + expiryTime + '\'' +
                            ", accessType='" + accessType + '\'' +
                            '}';
                }

                public String getAccessName() {
                    return accessName;
                }

                public void setAccessName(String accessName) {
                    this.accessName = accessName;
                }

                public String getAccessId() {
                    return accessId;
                }

                public void setAccessId(String accessId) {
                    this.accessId = accessId;
                }

                public String getExpiryTime() {
                    return expiryTime;
                }

                public void setExpiryTime(String expiryTime) {
                    this.expiryTime = expiryTime;
                }

                public String getAccessType() {
                    return accessType;
                }

                public void setAccessType(String accessType) {
                    this.accessType = accessType;
                }
            }

ProfileController

// ---------------------- UPDATE LIST OF PROFILES
        @RequestMapping(value="/profiles/batch", method=RequestMethod.POST)
        public void updateProfile(@RequestBody List<Profile> profiles) {

            logger.debug(profiles.get(0).toString());

            profileService.updateProfiles(profiles);
        }

ProfileService

public void updateProfiles(List<Profile> profiles) {

        datastore = MongoDbHelper.INSTANCE.getDatastore();

        for (Profile profile : profiles) {

            logger.debug(profile.toString());

            datastore.save(profile);
        }

    }

MongoDbHelper

   private MongoDbHelper() {

            MongoClient mongoClient = new MongoClient(new MongoClientURI("uritomongodb"));
            Morphia morphia = new Morphia(); 
            this.datastore = morphia.createDatastore(mongoClient, DATABASE_NAME);


        }

        public Datastore getDatastore() {
            return this.datastore;
        }

Posted JSON

[{"id":{"timestamp":1489743145,"machineIdentifier":13056160,"processIdentifier":3851,"counter":6420585,"time":1489743145000,"date":1489743145000,"timeSecond":1489743145},"version":14,"userName":"test@gmail.com","profileData":null,"profileTags":null,"profileName":"jonas","profileAccess":[{"accessId":"testare","expiryTime":"20170319","accessName":"testare","accessType":"shop"}]}]

来源:https://stackoverflow.com/questions/42888396/cant-find-a-codec-for-class-morphia-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!