The first two articles of this tutorial introduced how to read and write access to MongoDB using Node.js, a normal Java application, and a SpringBoot application.
- One of a series of introductory MongoDB tutorials: development environment setup and read and write access to Node.js and Java
- MongoDB introductory tutorial series 2: Using Spring Boot to operate MongoDB
This article introduces another approach to manipulating MongoDB using Restful APIs.
Use SpringBoot to develop Restful API for read and write access to MongoDB
The Restful API is constructed through Spring Boot, so that MongoDB can be added, deleted, checked and modified by calling the Restful API directly in the browser.
Let's first look at the final effect achieved by following the steps introduced in this tutorial.
Suppose I have a table book in my local MongoDB database with only one record with an id of 1.
Read the record by id through this url in the browser: http://localhost:8089/bookmanage/read?id=1
Take the following url for record creation:
http://localhost:8089/bookmanage/create?id=2&name=Spring&author=Jerry
The url where the search operation is logged: http://localhost:8089/bookmanage/search?name= *
Delete record: delete the record with id 2
http://localhost:8089/bookmanage/delete?id=2
The following are the implementation details of the above functions.
- Create a new controller under the folder src/main/java.
This controller is annotated @RestController
.
The @RestController annotation is equivalent to the union of the functionality provided by the @ResponseBody
and @Controller
annotations.
A point of knowledge here is that if you define a Controller with the annotation @RestController, the method in this Controller cannot return the jsp page or the html page, because the @ResponseBody annotation is working. So even if the view resolver InternalResourceViewResolver is configured, it will not take effect, and the content returned at this time is the content returned in the controller method defined by @RestController.
- Taking the read operation as an example, the url of the Restful API for the read operation is defined as
@GetMapping
/read.
@RequestParam defines url: the parameter after bookmanage/read is id or name.
The read operation will eventually use the Java method we introduced earlier to access MongoDB, that is, through the @Autowired
injected BookRepository instance to complete the operation on MongoDB.
- Source code for the create action:
@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;
}
- Source code for 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;
}
How to use MongoDB on SAP Cloud Platform
I would like to use the MongoDB service in the Cloud Foundry environment of the SAP cloud platform, but I cannot find it on the Service Marketplace.
The command line cf marketplace
returns no MongoDB related services either:
solution
Go back to the Global Account of the SAP cloud platform, click Entitlement, and find that MongoDB is not allocated to Subaccount:
Quota is assigned to 1, and you can save it.
Go back to the organization space and find that the MongoDB service is now available.
The command line cf marketplace
can now see the version of the MongoDB service:
Below are the detailed steps to use the MongoDB service in the SAP cloud platform CloudFoundry environment.
- Use the command line
cf marketplace
to view the version number of the MongoDB service of the current SAP cloud platform: On the SAP cloud platform I use, it isv3.0-dev
:
Create a service instance using the following command line:
cf create-service mongodb v3.0-dev mongo-service
- Go to the Java project root directory and use the command line
mvn package
:
You can see the following output:
This command generates a hcp-cf-mongodb-tutorial-1.0-SNAPSHOT.jar
:
- Edit
manifest.yml
to specify the name of the program deployed to the SAP cloud platform:
- Use the command line
cf push
to deploy.
After the deployment is successful, you can see that the application is in the Started state in Cockpit:
When a browser accesses the application, you can see the following output:
This output is generated by the RootController in the Java project:
We conclude this series with a comparison of the SAP UI5 Repository and the MongoDB Repository.
UI5 Repository
The UI5 application uploaded to the ABAP gateway system from the local Eclipse team provider will automatically generate a UI5RepositoryPathMapping.xml
file, which records the mapping relationship between the script file name and path of the debug version. As shown below:
The name of the manipulation utility that controls the access to this mapping file:
UI5/CL_UI5_REP_PATH_MAPPER
MongoDB Repository in SpringBoot application
Unlike ADBC and JDBC, accessing the MongoDB database through the MongoDB Repository does not include SQL statements spliced with strings in the code:
For example, we look at the definition of findByName
:
The implementation of findByName: The 20th line of code constructs a Query instance, and then passes it into the 21st line mongoTemplate
find
:
Summarize
This article first introduces how to use SpringBoot applications to develop Restful APIs, so that the read and write operations of MongoDB can be tested in the browser, and then how to consume MongoDB services in the general cloud platform CloudFoundry environment.
The first two articles in this tutorial:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。