java - Spring @Builder annotation is not providing builder() method - Stack Overflow

admin2025-04-17  4

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

Share Improve this question edited Jan 30 at 17:46 Mr. Polywhirl 48.9k12 gold badges94 silver badges145 bronze badges asked Jan 30 at 17:31 Mahadi HasanMahadi Hasan 298 bronze badges 6
  • Did you look at the class files that Lombok generated? – Mr. Polywhirl Commented Jan 30 at 17:46
  • @Mr.Polywhirl, I don't get your question – Mahadi Hasan Commented Jan 30 at 18:01
  • 1 In an IDE like JetBrains IntelliJ, you can view the decompiled class object. – Mr. Polywhirl Commented Jan 31 at 0:55
  • I am using STS. What's the solution? – Mahadi Hasan Commented Jan 31 at 5:15
  • have you tried to download Lombok plugin for sts ? – kush parsaniya Commented Jan 31 at 5:38
 |  Show 1 more comment

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://anycun.com/QandA/1744901842a89231.html