NOTE Anyone else reading this who isn't me, can treat the steps here as the stuff you should try out before looking at the solutions.
In this project, you will be building a log management application using Java and Spring Boot.
The application will be a simple RESTful API that accepts log entries, stores them, and provides basic
search and retrieval functionality.
The front end will be minimal, using basic HTML and JavaScript to interact with the API.
The project will be broken down into the following deliverables:
- Setup and create a "Hello, World!" Spring Boot application.
- Design and implement a basic log entry data model.
- Create a simple in-memory storage for log entries.
- Implement RESTful API endpoints for adding, retrieving, and searching log entries.
- Add basic exception handling and validation for the API.
- Implement persistence using a database (e.g., MySQL or PostgreSQL).
- Add authentication and authorization to the API.
- Create a minimal front end to interact with the API.
As your teacher, I recommend you to learn and complete the following tasks for this deliverable:
- Install Java Development Kit (JDK) and set up your Java environment.
- Familiarize yourself with Java syntax and basic concepts, such as classes, objects, and methods.
- Set up a development environment (e.g., IntelliJ IDEA or Eclipse).
- Learn the basics of Spring Boot and its advantages over traditional Java web applications.
- Create a new Spring Boot project using Spring Initializr (https://start.spring.io/).
- Add a "Hello, World!" RESTful API endpoint to the project.
- Run the application locally and test the "Hello, World!" endpoint using a tool like Postman or curl.
- Once you have completed these tasks, provide the source code for the "Hello, World!" Spring Boot application, and we will move on to the next deliverable.
For this deliverable, you will need to create a data model to represent log entries in your application. Each log entry should have a timestamp, a log level (e.g., INFO, WARNING, ERROR), and a message.
As your teacher, I recommend you to learn and complete the following tasks for this deliverable:
- Learn about Java classes, constructors, and getter/setter methods.
- Familiarize yourself with Java Enums to represent the log levels.
- Create a LogEntry class that contains the following fields:
- A unique identifier (e.g., UUID or long)
- A timestamp (e.g., LocalDateTime or Instant)
- A log level (using an Enum)
- A message (String)
- Implement constructors, getters, and setters for the LogEntry class.
Once you have completed these tasks, provide the source code for the LogEntry class, and we will move on to the next deliverable.
Create a LogService
class that will be responsible for managing log entries. In this class, define a List<LogEntry>
as an in-memory storage for your log entries. Implement the following methods:
addLogEntry(LogEntry entry)
: Add a new log entry to the list.List<LogEntry> getAllLogEntries()
: Return the list of all log entries.Optional<LogEntry> getLogEntry(UUID id)
: Return a log entry by its UUID, wrapped in anOptional
object.boolean deleteLogEntry(UUID id)
: Delete a log entry by its UUID. Returntrue
if the log entry was found and deleted,false
otherwise.Optional<LogEntry> updateLogEntry(UUID id, LogEntry newEntry)
: Update a log entry by its UUID with the new entry provided. Return the updated log entry wrapped in anOptional
object if the update was successful, or an emptyOptional
if the log entry was not found.
Annotate the LogService
class with @Service
to mark it as a Spring Service component.
In your HelloWorldController
, inject an instance of LogService
using the @Autowired
annotation.
Create new RESTful API endpoints in your HelloWorldController
to perform the following operations:
- Add a new log entry (HTTP POST request)
- Get all log entries (HTTP GET request)
- Get a log entry by UUID (HTTP GET request)
- Delete a log entry by UUID (HTTP DELETE request)
- Update a log entry by UUID (HTTP PUT request)
For each endpoint, use appropriate request methods, paths, and path variables, and return appropriate HTTP status codes for the operation's result.
SOLUTION
Create HelloWorldController.java
at same level
right click, add javaclass
package com.example.logManagerApp; // annotation indicates that this class is a RESTful controller.
import org.springframework.web.bind.annotation.GetMapping; //annotation maps HTTP GET requests
import org.springframework.web.bind.annotation.RestController; //
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String helloWorld() {
return "Hello, World!";
}
}
Now i can hit localhost:8090/hello
hello is defined in hte @GetMapping("/hello")
Do this in application.properties file
server.port=8090
Must have the same name as the class
Don't give it a type, its just
public Car(String make, String model, int year){}