EzyHTTP Handle Requests
Updated at 1782574372000EzyHTTP Handle Requests, EzyHTTP allows you to handle client requests via Controller methods with HTTP methods such as GET, POST, PUT, and DELETE.
EzyHTTP maps an incoming HTTP request to a controller method by matching the request path and HTTP method. Inside that method, you can read values from path variables, query parameters, or request bodies, then return plain objects or
ResponseEntity as the response.
flowchart LR
Client[Client] --> Request[HTTP Request]
Request --> Router[EzyHTTP Router]
Router --> Controller[Controller Method]
Controller --> Service[Service Layer]
Service --> Controller
Controller --> Response[HTTP Response]
Response --> Client
Handle Get Request
You can handle a GET request by creating a controller method and annotating it with
@DoGet.GET requests are commonly used to retrieve resources. EzyHTTP allows you to pass request data through path variables or query parameters.
Use path variables when the parameter identifies a resource:
@DoGet("/books/{bookId}") public BookResponse getBook(@PathVariable Long bookId) { final BookData bookData = bookService.getBook(bookId); return dataToResponseConverter.toResponse(bookData); }
In the example above,
bookId is part of the URL path. A request such as:GET /books/1
will call
getBook with bookId = 1.Use query parameters when the request has filtering, sorting, pagination, or optional conditions:
@DoGet("/books") public List<BookResponse> getBooks( @RequestParam("lower_than") String lowerThan, @RequestParam("upper_than") String upperThan, @RequestParam("size") int size ) { final List<BookData> dataList = bookService.getBooks( lowerThan, upperThan, size ); return dataToResponseConverter.toResponseList(dataList); }
A matching request can look like this:
GET /books?lower_than=10&upper_than=100&size=20
Path variables make APIs more readable and resource-oriented, especially when the URL points to one specific resource. Query parameters are better when the client needs to describe how a collection should be searched or filtered.
You can find the full source code in BookController Example.
Handle Post Request
POST requests are commonly used to create resources or trigger operations that change server state.
A POST request can still use path variables and query parameters, but it is also suitable for receiving a request body.
@DoPost("/book/add/{count}") public boolean addBooks(@PathVariable int count) { bookService.addBooks(count); return Boolean.TRUE; }
The example above uses a path variable to tell the server how many books should be added.
For APIs that create a resource from client-provided data, you can use
@RequestBody:@DoPost("/book/add") public BookResponse addBook(@RequestBody AddBookRequest request) { bookValidator.validate(request); final AddBookData addBookData = requestToDataConverter.toData(request); final BookData bookData = bookService.addBook(addBookData); return dataToResponseConverter.toResponse(bookData); }
A request body can be JSON, for example:
{
"name": "Clean Architecture",
"author": "Robert C. Martin",
"price": 42
}
With
@RequestBody, EzyHTTP converts the HTTP body into a Java object, so the controller method can work with a typed request model instead of parsing raw input manually.You can find the full source code in BookController Example.
Handle Put Request
PUT requests are usually used to update an existing resource.
A common pattern is to use a path variable for the resource ID and a request body for the update data:
@DoPut("/books/{bookId}") public ResponseEntity updateBook( @PathVariable Long bookId, @RequestBody UpdateBookRequest request ) { bookService.updateBook(bookId, requestToModelConverter.toModel(request)); return ResponseEntity.noContent(); }
A matching request can look like this:
PUT /books/1 Content-Type: application/json
{
"name": "Domain-Driven Design",
"price": 55
}
Returning
ResponseEntity.noContent() is useful when the update succeeds but the API does not need to return the updated resource. In HTTP terms, this usually means the server can respond with status 204 No Content.Handle Delete Request
DELETE requests are commonly used to remove a resource.
For delete APIs, the resource ID should usually be passed as a path variable:
@DoDelete("/books/{bookId}") public ResponseEntity deleteBook(@PathVariable Long bookId) { bookService.delete(bookId); return ResponseEntity.noContent(); }
A matching request can look like this:
DELETE /books/1
Using a path variable keeps the API clear: the URL identifies the resource, and the HTTP method describes the action.
You can find the full source code in BookController Example.
Choosing Path Variables, Query Parameters, and Request Body
Each input style has a different purpose:
| Input type | Best for | Example |
|---|---|---|
| Path variable | Identifying a specific resource | /books/{bookId} |
| Query parameter | Filtering, searching, sorting, pagination | /books?size=20 |
| Request body | Sending structured data | JSON create/update request |
A simple rule is:
flowchart TD
A[Need to pass data to controller?] --> B{Does it identify a resource?}
B -->|Yes| C[Use @PathVariable]
B -->|No| D{Is it filter/search/pagination data?}
D -->|Yes| E[Use @RequestParam]
D -->|No| F[Use @RequestBody]
Recommended Controller Style
A controller method should focus on HTTP input and output. Business logic should stay in the service layer.
A clean request-handling flow usually looks like this:
sequenceDiagram
participant Client
participant Controller
participant Validator
participant Service
participant Converter
Client->>Controller: Send HTTP request
Controller->>Validator: Validate request
Controller->>Converter: Convert request to data/model
Controller->>Service: Execute business logic
Service-->>Controller: Return result
Controller->>Converter: Convert result to response
Controller-->>Client: Return HTTP response
This style keeps the controller easy to read and test:
@DoPost("/books") public BookResponse createBook(@RequestBody AddBookRequest request) { bookValidator.validate(request); final AddBookData addBookData = requestToDataConverter.toData(request); final BookData bookData = bookService.addBook(addBookData); return dataToResponseConverter.toResponse(bookData); }