Search code examples
spring-bootintellij-ideagetter-setterlombok

IntelliJ / Lombok messing around with "is" prefix and removing it


I have no idea what is happening but for some reason the is prefix from my property is being removed.

I have even removed the annotations from Lombok and manually created the getters and setters but for some reason, I get this weird error.

I have a prop called isAuthenticated, that must return to the browser, for some reason, I am getting authenticated.

Here is the code: //The end point in the API: @GetMapping("/auth") public EmployeeState getEmployeeState(HttpServletRequest request) { return employeeService.getEmployeeState(request.getSession()); }

The Service method:

public EmployeeState getEmployeeState(HttpSession session) {

        EmployeeState employeeState = new EmployeeState();
        employeeState.setIsAuthenticated(SecurityContextHolder.getContext().getAuthentication().isAuthenticated());

        if (employeeState.isAuthenticated()){
            if (session.getAttribute("employeeNumber") != null){
                Employee employee = employeeRepository.getEmployeeByEmployeeNumber(session.getAttribute("employeeNumber").toString()).get();
                employeeState.setFirstName(employee.getFirstName());
                employeeState.setLastName(employee.getLastName());
                employeeState.setEmployeeNumber(employee.getEmployeeNumber());
            }
        }

        return employeeState;
    }

This is the model, the response is supposed to return:

public class EmployeeState {

    private String firstName;
    private String lastName;
    private String employeeNumber;
    private boolean isAuthenticated;

    public EmployeeState(){

    }

    public EmployeeState(String firstName, String lastName, String employeeNumber, boolean isAuthenticated) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.employeeNumber = employeeNumber;
        this.isAuthenticated = isAuthenticated;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(String employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public boolean isAuthenticated() {
        return isAuthenticated;
    }

    public void setIsAuthenticated(boolean authenticated) {
        isAuthenticated = authenticated;
    }
}

Result in the browser:

Why is isAuthenticated returning as authenticated?

Edit 1:

I tried to add the @JsonProperty annotation, and now I get two properties:

public class EmployeeState {

    private String firstName;
    private String lastName;
    private String employeeNumber;

    @JsonProperty(value = "isAuthenticated")
    private boolean isAuthenticated;

    public EmployeeState(){

    }

    public EmployeeState(String firstName, String lastName, String employeeNumber, boolean isAuthenticated) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.employeeNumber = employeeNumber;
        this.isAuthenticated = isAuthenticated;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmployeeNumber() {
        return employeeNumber;
    }

    public void setEmployeeNumber(String employeeNumber) {
        this.employeeNumber = employeeNumber;
    }

    public boolean isAuthenticated() {
        return isAuthenticated;
    }

    public void setIsAuthenticated(boolean authenticated) {
        isAuthenticated = authenticated;
    }
}

Now there are two properties:

Now it shows two properties


Solution

  • The correct Java Bean naming convention for your getter method would be isIsAuthenticated. Jackson tries to use the Java bean convention, and in this case, your class doesn't conform to it. You can try adding the @JsonProperty(value = "isAuthenticated") annotation on the isAuthenticated method directly. Additionally, to prevent any duplicate fields in the JSON, you could then add @JsonIgnore on the isAuthenticated field, if you don't need to serialize that field from JSON.

    Small explanation why you have 2 fields in JSON now:

    The correct property name, that Jackson derives from your isAuthenticated() method is authenticated. The property name is derived such that the getter prefix (which is is for boolean methods) is removed and the following letter converted to lower case). That's why you see the JSON property authenticated.

    Additionally Jackson sees your isAuthenticated field, and creates a JSON property for it as well, which is then named isAuthenticated. By adding the @JsonProperty annotation on the field itself, nothing changes for Jackson, because for Jackson, the isAuthenticated() method has no relation at all to the field and setter method and thus they are treated as two different JSON properties.