I am writing some custom exception methods with a custom Response class. This is my Response Class
package com.mahadi.InventoryManagementSystem.dto;
import java.time.LocalDateTime;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.mahadi.InventoryManagementSystem.enums.UserRole;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
@JsonInclude(content = Include.NON_NULL)
public class Response {
// generic
private int status;
private String message;
// for login
private String token;
private String expirationTime;
private UserRole role;
// for pagination
private Integer totalPages;
private Long totalElement;
// data output optional
private UserDTO user;
private List<UserDTO> userList;
This is my Global Exception handler class
package com.mahadi.InventoryManagementSystem.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.mahadi.InventoryManagementSystem.dto.Response;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Response> handleAllExceptions(Exception exception) {
Response response = Response.builder()
.status(HttpStatus.INTERNAL_SERVER_ERROR.value()) // or other status code
.message(exception.getMessage()) // exception message
.build();
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Now look at the screenshot. Here the builder method is not responding, but I added the @Builder annotation in the Response class. I failed to fix it. I need your suggestion. I am using JDK 17 with spring boot 3.0.0
I am writing some custom exception methods with a custom Response class. This is my Response Class
package com.mahadi.InventoryManagementSystem.dto;
import java.time.LocalDateTime;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.mahadi.InventoryManagementSystem.enums.UserRole;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
@JsonInclude(content = Include.NON_NULL)
public class Response {
// generic
private int status;
private String message;
// for login
private String token;
private String expirationTime;
private UserRole role;
// for pagination
private Integer totalPages;
private Long totalElement;
// data output optional
private UserDTO user;
private List<UserDTO> userList;
This is my Global Exception handler class
package com.mahadi.InventoryManagementSystem.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.mahadi.InventoryManagementSystem.dto.Response;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<Response> handleAllExceptions(Exception exception) {
Response response = Response.builder()
.status(HttpStatus.INTERNAL_SERVER_ERROR.value()) // or other status code
.message(exception.getMessage()) // exception message
.build();
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Now look at the screenshot. Here the builder method is not responding, but I added the @Builder annotation in the Response class. I failed to fix it. I need your suggestion. I am using JDK 17 with spring boot 3.0.0
If you have a custom constructor, Lombok might not generate the builder method unless you specify @Builder on the constructor itself. Try rebuilding the Project: After making changes, clean and rebuild your project to ensure that Lombok's annotations are processed correctly.