gpt4 book ai didi

java - POJO 中 JSONObject 字段的 SpringBoot 和 MongoDB 保存问题

转载 作者:行者123 更新时间:2023-12-05 06:14:35 26 4
gpt4 key购买 nike

我有一个类似于下面的简单 POJO。为简洁起见,省略了 getter 和 setter。

@Document(collection = "posts")
public class Post {
@Id
public String id;

@Field(name = "author")
public String author;

@Field(name = "title")
public String title;

@Field(name = "body")
public JSONObject body;

public Post() {

}

public Post(String id, String author, String title, JSONObject body) {
this.id = id;
this.title = title;
this.author = author;
this.body = body;
}

我使用 MongoRepository 的默认保存来保存我的数据:

import org.springframework.data.mongodb.repository.MongoRepository;

public interface PostRepository extends MongoRepository<Post, String> {

}

使用以下代码将其保存到 MongoDB:

@PostMapping
public Post addPost(HttpEntity<String> httpEntity) {
try {
JSONObject obj = new JSONObject(httpEntity.getBody());

Post p1 = new Post("123", obj.getString("author"), obj.getString("title"),
obj.getJSONObject("body"));

return postRepository.save(p1);
} catch (Exception e) {
e.printStackTrace();
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Foo Not Found", e);
}
}

当它保存在 MongoDB 中时,它存储了原本不在我应该保存的 JSON 中的字段。

原始 JSON:

{
body: {
blocks: {
[
{ data: "Data 1", type: "Type 1" },
{ data: "Data 2", type: "Type 2" },
]
},
version: "1.2.2"
}
}

保存的 JSON:

{
body: {
map: {
blocks: {
myArrayList:
[
{ map: { data: "Data 1", type: "Type 1" }, _class: "org.json.JSONObject" },
{ map: { data: "Data 2", type: "Type 2" }, _class: "org.json.JSONObject" },
],
_class: "org.json.JSONArray"
},
version: "1.2.2"
}
}
}

当我尝试获取数据时,收到以下错误:

java.lang.IllegalStateException: Cannot set property map because no setter, no wither and it's not part of the persistence constructor public org.json.JSONObject()!

这是我获取数据的方式:

@GetMapping(path = "/{id}")
public Post getPost(@PathVariable String id) {
try {
Optional<Post> post = postRepository.findById(id);

return post.orElseThrow();
} catch (Exception e) {
e.printStackTrace();
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Foo Not Found", e);
}
}

我的第一个问题是,有没有办法在没有额外字段(例如 mapmyArrayList)的情况下将 JSON 对象保存到 MongoDB?

其次,我应该如何使用 POJO 中的 JSONObject 字段检索数据?

提前致谢!

最佳答案

这是存储任何文档的通用解决方案

package io.innoit.elearning.generic;

import java.util.List;

import org.bson.Document;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/generic")
@CrossOrigin(origins = { "*" })
public class GenericController {

@Autowired
private MongoTemplate mongoTemplate;

@PostMapping
public ResponseEntity<Document> addData(@RequestBody String data) {
JSONObject jsonObject = new JSONObject(data);
String documentName = jsonObject.getString("documentName");
Document doc = Document.parse(data);
Document insertedDoc = mongoTemplate.insert(doc, documentName);
return new ResponseEntity<>(insertedDoc, HttpStatus.CREATED);

}

@GetMapping("/{documentName}")
public List<Document> getData(@PathVariable String documentName) {
List<Document> allData = mongoTemplate.findAll(Document.class, documentName);
return allData;
}

}

关于java - POJO 中 JSONObject 字段的 SpringBoot 和 MongoDB 保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62829710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com