簡體   English   中英

具有MongoDB POST和GET記錄的Spring控制器

[英]Spring controller with MongoDB POST and GET records

我需要幫助,以了解當我想在數據庫中發布某些內容並進行檢索時如何以及為什么要使用控制器。 當我僅使用Post.java和PostRepository.java時,在我插入數據以及在“ / posts”路徑中檢索整個數據庫時,它似乎可以工作。 在文件中,我有一個包含所有條目的posts數組。

Post.java

public class Post {

@Id private long id;

private String content;
private String time;
private String gender;  
private String age;


// Constructors

public Post() {

}

public Post(long id, String content, String time, String gender, String age) {
    this.id = id;
    this.content = content;
    this.time = time;
    this.gender = gender;
    this.age = age;
}

// Getters

public String getContent() {
    return content;
}

public long getId() {
    return id;
}

public String getTime() {
    return time;
}

public String getGender() {
    return gender;
}

public String getAge() {
    return age;
}

PostRepository.java

@RepositoryRestResource(collectionResourceRel = "posts", path = "posts")
public interface PostRepository extends MongoRepository<Post, String> {

List<Post> findPostByContent(@Param("content") String content);
}

PostController.java

@RestController
public class PostController {    

private final AtomicLong counter = new AtomicLong();

//Insert post in flow 
@RequestMapping(value="/posts", method = RequestMethod.POST)
public Post postInsert(@RequestBody Post post) {
    return new Post(counter.incrementAndGet(), post.getContent(), post.getTime(), post.getGender(), post.getAge());
}

//Get post in flow
@RequestMapping(value="/posts", method = RequestMethod.GET)
public Post getPosts() {
    return null;  //here I want to return all posts made using postInsert above.
}
}

當我使用控制器時,我可以發布數據,但不會將其保存在json文件中,因此,當我重新啟動應用程序時,我將從id:1重新開始。 但是,如果沒有控制器,數據將被保存。 為什么會這樣呢? 我該如何安排以便與控制器一起保存數據? 我知道這可能是一個愚蠢的問題,但我不知道該怎么辦。

控制器在這里用於管理通過您的應用發出的請求。 無論您使用@RestController還是@Controller,都會得到完全不同的結果。

因此,REST方法是將對象視為資源。 JSON是表示您要公開的資源的默認格式。

使用PostRepository之前,您必須在數據中正確保存數據,然后再使用return語句將它們暴露出來(例如return僅以JSON格式顯示資源(您的帖子)。要保存帖子,您必須對MongoRepository使用save()方法)

    //Insert post in flow 
@RequestMapping(value="/posts", method = RequestMethod.POST)
public Post postInsert(@RequestBody Post post) {

// you have to use your repository to be able to CRUD operations in the store

final PostRepository postRepository;    

// save the post in the store

postRepository.save(new Post(counter.incrementAndGet(), post.getContent(), post.getTime(), post.getGender(), post.getAge()));

HttpHeaders httpHeaders = new HttpHeaders();
                    httpHeaders.setLocation(ServletUriComponentsBuilder
            .fromCurrentRequest().build().toUri());

return new ResponseEntity<>(null, httpHeaders, HttpStatus.CREATED);
}

ResponseEntity用於表示整個HTTP響應。 您可以控制其中的所有內容:狀態碼,標頭和正文。

要獲取商店中的所有帖子,請使用存儲庫作為保存數據。

    //Get post in flow
@RequestMapping(value="/posts", method = RequestMethod.GET)
public Post getPosts() {
    return this.postRepository.findAll();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM