向运行Spring BOOT的tomcat发布JSON。
通过Morphia映射请求更新Mongo
获取错误:“状态”:500,“错误”:“内部服务器错误”,“异常”:“org.bson.codecs.configuration.CodecConfigurationException”,“message”:“无法找到编解码器class se.preffo.model.ProfileAccess。
不知何故,它不知道如何创建Object ProfileAccess
ProfileClass
包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类:
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;
}
发布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"}]}]