In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.