Get the App
SLTechnology News&Howtos  ›  Database  › 

How to access MongoDB through Restful API

Shulou Source: shulou.com Published: 2022-05-31 12:13:04 09月13日 Update

This article introduces the knowledge of "how to access MongoDB through Restful API". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Let's look at the results first. Suppose there is a table book in my local MongoDB database, there is only one record, and the id is 1.

Read the record according to id through the url in the browser: http://localhost:8089/bookmanage/read?id=1

Creation of the record:

Http://localhost:8089/bookmanage/create?id=2&name=Spring&author=Jerry

Search for records: http://localhost:8089/bookmanage/search?name=*

Deletion of records: delete records with id 2

Http://localhost:8089/bookmanage/delete?id=2

Here are the details of the implementation.

1. Create a new controller under the folder src/main/java.

This controller is annotated with @ RestController. The @ RestController annotation is equivalent to the union of the functions provided by the @ ResponseBody and @ Controller annotations. One point of knowledge here is that if you define a Controller with the annotation @ RestController, then the method in this Controller cannot return the jsp page or html, because the @ ResponseBody annotation works, so even if the view parser InternalResourceViewResolver is configured, it will not take effect. The content returned at this time is the content returned in the controller method defined by @ RestController.

two。 Taking the read operation as an example, the url of the read operation Restful API is defined as bookmanage/read by annotating @ GetMapping.

@ RequestParam defines the parameter after url:bookmanage/read as id or name. The read operation will eventually use the method we introduced in the third of the simplest introductory tutorials for MongoDB, using Java code to insert data into MongoDB, that is, to complete the operation on MongoDB through the BookRepository instance injected by @ Autowired.

3. Create the source code for the operation:

@ GetMapping ("/ bookmanage/create") public Book create (@ RequestParam (value= "id", defaultValue= "") String id,@RequestParam (value= "name", defaultValue= "noname") String name,@RequestParam (value= "author", defaultValue= "noauthor") String author) {Book book = repository.save (new Book (id,name,author)); return book;}

4. The source code for the delete operation:

@ GetMapping ("/ bookmanage/delete") public boolean delete (@ RequestParam (value= "id", defaultValue= "") String id) {/ / if no record if (repository.findById (id) = = null) return false; / / do database delete repository.deleteById (id); return true;}

This is the end of "how to access MongoDB through Restful API". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Annotations content methods knowledge that is data more source code practical learned and then two code function introductory tutorials functions parameters only dilemma examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Xiaomi MariaDB NVidia