0% found this document useful (0 votes)
27 views2 pages

Light-4j User REST API Design

The document outlines a task to design a simple JAVA web server/application with 6 REST endpoints using Light-4j and MongoDB. It specifies the functionality of each endpoint, including loading users, creating, retrieving, updating, and deleting user data, with a focus on adhering to REST best practices. Additionally, it provides guidelines for submission and hints for development resources.

Uploaded by

sanjay raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Light-4j User REST API Design

The document outlines a task to design a simple JAVA web server/application with 6 REST endpoints using Light-4j and MongoDB. It specifies the functionality of each endpoint, including loading users, creating, retrieving, updating, and deleting user data, with a focus on adhering to REST best practices. Additionally, it provides guidelines for submission and hints for development resources.

Uploaded by

sanjay raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Backend Interview

Task Description
You will design a simple JAVA web server/application, having a set of 6 REST endpoints, and using MongoDB to
store the details. The server should be written in Light-4j, a lightweight Java web framework.

Dummy data APIs for your use are available at JSON PLaceholder. You will only use the /users endpoint for your
code.

Quickstarts and References linked below:

1. Light-4j
2. MongoDB
3. REST API

Rest Endpoints

Create a Rest service using light-rest-4j (either use light-codegen with a simple openapi3 spec OR
create from scratch)
No need to use any authentication for MongoDB

HTTP
URL Description
VERB

Loads 10 users into DB, should be called automatically on application startup as


GET /load well, user data should be fetched from [JSON PLaceholder], should always return
empty response - with code 200 on success, else an appropriate error code.

DELETE /users Deletes all users in the DB

DELETE /users/:userId Delete user with userId from DB

GET /users/:userId Get the specific user with userId from DB and return its data in json format

Puts a new user into the DB, the data is sent to the server as the request body of
PUT /users this api, should return appropriate error code if userId already exists; See the user
data format below.

Updates an existing user in the DB, the data is sent to the server as the request
POST /users/:userId body of this api, should return appropriate error code if userId doesn't exist; See
the user data format below.

Certain details (like the response for some endpoints) are deliberately left empty for you to design according to
best REST practices. Few things for you to think about:
Error code and response if a userId doesn't exist when using GET/DELETE
Error code if POST body has different userId field that the one it tries to update. Should it even have a
userId in the POST body ?
link header in response of POST/PUT

User Data Format

{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@[Link]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "[Link]",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}

Hints

Some resources which may help duirng development:

Light-4J Examples Repository


Light-4J Cross Cutting Concerns
Light-4J Body Parser

Submission Guidelines
Please do not upload to any public source code repository like github
Please zip up your code and upload it to your google_drive and share its link with us
The zip should include the Light4J services created in the one project.

Common questions

Powered by AI

Challenges in integrating a Java web server with MongoDB to handle user data include managing schema designs due to the flexible nature of NoSQL, ensuring data consistency, and handling connection pooling efficiently. These can be addressed by adhering to best practices like defining schemas using techniques such as Mongoose for Node.js frameworks when using MongoDB, implementing transactions for atomic operations, and utilizing MongoDB’s built-in mechanisms for connection pooling to manage performance effectively. Additionally, employing robust error handling and validation can prevent data integrity issues .

Error codes and messages enhance API usability by providing clear and precise feedback about why a request has failed, which can help developers understand the issue without delving into server logs. For example, a 404 error code indicates that the requested resource is not found, while a 409 error indicates a conflict, such as attempting to PUT a resource that already exists . Detailed error messages also assist in debugging by providing additional context about the problem, thus facilitating faster resolution .

In a RESTful API, the PUT request body technically need not include a userId because the resource URI already represents the unique identifier for the resource being updated. However, including the userId in the request body could help maintain data consistency across client-server interactions by ensuring that the server updates the correct record. If included, the server must verify that the userId in the request body matches the userId in the URI, returning an error code (e.g., 400 Bad Request) if there's a discrepancy .

In a RESTful service, when attempting to delete a nonexistent user record using the DELETE method, the service should return a 404 Not Found status code. This is because the resource that the client attempted to delete does not exist. It is important to follow this convention to maintain consistency and clarity in API design .

Automating the loading of user data into the database upon application startup ensures that test or sample data is readily available for immediate application use, streamlining both development and testing phases. It assists in recurring initialization tasks, reducing manual errors and ensuring that the application environment is consistently set up. This practice can also be beneficial for setting up demo environments or resetting application states during integration testing .

Using a lightweight Java web framework such as Light-4j for developing RESTful APIs allows for faster performance and lower resource consumption compared to more heavyweight frameworks. This is because Light-4j is designed to optimize throughput and minimize latency, which is beneficial for applications requiring high concurrency and low overhead . Additionally, Light-4j's modular design enables developers to include only the components they need, reducing the overall complexity and memory footprint of the application .

Dummy data APIs like JSON Placeholder facilitate the testing and development of RESTful services by providing developers with realistic data to use without needing to set up their own databases or API endpoints. This supports the creation of mock services, enabling developers to test the functionality of API calls and handle various responses in a controlled environment. This is particularly useful for frontend-backend integration and testing data flow scenarios without the complexities and time investment involved in managing live data .

Returning a 200 status code with an empty response in a RESTful service during successful data loading operations is crucial for confirming the completion of the task without returning unnecessary data, which aligns with REST principles. The focus of the operation is action execution, and the 200 status code effectively communicates success to the client, ensuring clear and efficient communication regarding the operation's outcome .

When choosing between POST and PUT for user data updating in REST API design, consider the nature of the operation. Use PUT when you need to update a resource at a known URI, ensuring that the data replaces or updates the existing data completely. PUT is idempotent, making it suitable for updates that can be repeated safely. Use POST when the update operation should result in a subordinate to the resource or when the operation could result in a new resource being created if it does not already exist; POST is non-idempotent and allows for more flexible operations .

MongoDB offers several advantages over traditional relational databases in storing RESTful API data. As a NoSQL database, MongoDB provides more flexibility in data modeling; it uses a document-oriented data model that can easily accommodate changes in the data structure without needing complex schema migrations, which is useful in agile environments. Furthermore, MongoDB's JSON-like documents align well with the typical JSON data exchange format in RESTful APIs, facilitating easier data handling and manipulation .

You might also like