我得到了什么
这是我在mongoDB中保存的对话框集合
"_id": "5a61c7c7d5981a4b88b5fe17",
"_class": "com.kalsym.next.gen.campaign.data.Dialog",
"message": {
"title": "Welcome Message",
"subTitle": "Click SUbscribe to avail the offer",
"url": "www.hotmail.com"
},
"options": [{
"IR": "Thanks for subscription",
"value": "sub",
"path": "sub"
},
{
"IR": "www.kalsym.com/NoThanks",
"value": "NoThnks",
"path": "NT"
}]
我想要的是什么:
"_id": "5a4e16547d4cea1604bf28f8",
"message": {
"Title": "",
"SubTitle": "Do you want to talk to CSR?",
"URL": ""
},
"options": {
"Yes": {
"IR": "You will get a callback within 1 hour, thanks.",
"value": "Yes",
"path": "NT\\NotNow\\Yes"
},
"No": {
"IR": "No Problem, have a good day!",
"value": "No",
"path": "NT\\NotNow\\No"
}
}
问题在于“选项”字段。我希望它是一个键值对,而不是一个简单的列表。
这是我的Dialog类:
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Getter
@Setter
@Document(collection = "Dialog")
public class Dialog {
@Id
public String id;
private Message message;
private List<Option> options;
public Dialog(final Message message, final List<Option> options) {
this.message = message;
this.options = options;
}
}
这是Message类:
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Message {
private String title;
private String subTitle;
private String url;
public Message(final String title, final String subTitle, final String url) {
this.title = title;
this.subTitle = subTitle;
this.url = url;
}
}
这是选项类:
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Option {
private String IR;
private String value;
private String path;
public Option(final String IR, final String value, final String path) {
this.IR = IR;
this.value = value;
this.path = path;
}
}
以下是我如何保存它:
@SpringBootApplication
@EnableMongoRepositories(basePackageClasses = DialogRepository.class)
public class CampaignAPI implements CommandLineRunner {
@Autowired
private DialogRepository dialogRepository;
public static void main(String[] args) {
SpringApplication.run(CampaignAPI.class, args);
}
@Override
public void run(String... strings) throws Exception {
Message msg = new Message("Welcome Message", "Click SUbscribe to avail the offer", "www.hotmail.com");
final Option subscribe = new Option("Thanks for subscription", "sub", "sub");
final Option noThanks = new Option("www.kalsym.com/NoThanks", "NoThnks", "NT");
List<Option> options = Arrays.asList(subscribe, noThanks);
final Dialog welcomeDialog = new Dialog(msg, options);
dialogRepository.save(welcomeDialog);
}
}
我的问题
答案 0 :(得分:0)
这应该产生预期的JSON。
public class Dialog {
@Id public String id;
private Message message;
private Option Yes;
private Option No;
}
列表元素没有密钥,它们只在内存中有价值和位置。这就是你不想要List的原因。