Construct an IT Job website with SpringBoot
and MongoDB
.
Author: Dr. Yuhang Zhao
MongoDB is document-DB
.
Unlike relational database
,
it does not require creating table
before inserting data with pre-defined data structure.
With MongoDB we can store data with flexible structures. E.g., JSON
-like document.
Encountered problem: installation never ends on windows 7
X64 bit.
Solution: See "Install MongoDB With Compass On Windows"
@ https://vegibit.com/install-mongodb-with-compass-on-windows/
Open Command Prompt and navigate to your MongoDB Bin folder,
run mongod.exe
.
Encountered problem: Mongo Server
fails to up.
Solution: see "How to Install MongoDB on Windows 10"
@ https://www.youtube.com/watch?v=FwMwO8pXfq0
Then it will start MongoDB main process
and then keep waiting for connections message in the console.
Add mongoDB/bin
to Path
and we can use Mongo Shell
and Mongo Server
from anywhere.
-
Create a
controller
used inRESTful
web services to handleHTTP
requests and annotated by@RestController
. -
Create a
service
annotated by@Service
, which writes business logic in the service layer separated from@RestController
. -
Create the
entity
class for job posting and annotated by@Document
. Ref: https://schema.org/JobPosting -
Create the interface
repository
which extendsMongoRepository
using the entity class and itsId
type. -
Use
dependency injection
annotated by@Autowired
for the repository in service, similarly for service in controller. -
Implement the method to create job postings annotated by
@PostMapping
. Annotation@RequestBody
is used to bind theHTTP
request body with a domain object in the method parameter. -
So far we have implemented
Create
andRead
inCRUD
. We usePostMan
to test the methods.
- Below is what we obtain from
Http
Get
method:
-
Add "spring.data.mongodb.database=JobPostings" in
application.properties
. Thereafter, we use the database "JobPostings" to maintain collections which are corresponding to concepttables
inrelational DB
. -
Implement the method to get a job posting by id, and the method for deleting a job posting by id.
-
Implement the method to update a posting by id. First we need to introduce a new class
JobPostingNoId
which is the JobPosting class but without attributeId
. When calling the method to update a posting, beside the id we also have to supply an object ofJobPostingNoId
. In this way, we can update a posting except its id. The method returns the updated copy of the posting. Note that although the parameter is of typeJobPostingNoId
, it doesn't have to supply the complete list of attributes. See the following example.
Response from the method for updating:
- "What is MongoDB? Spring Data MongoDB in Spring Boot Application with Example" by "Tech Primers" @ https://www.youtube.com/watch?v=l5KC6OcbuOI
- "Spring Boot Tutorial - CRUD Operations using MongoDB" by "Code with Kris" @ https://www.youtube.com/watch?v=WT-2KWCa1l8