@Data Is Not Creating Constructor For Static Nested Class

Viewed 317

I am new in java world and need your help to resolve one issue.

Today I have created one maven spring project which is giving "The Constructor is undefined" error.

But my class has @Data annotation and I also added @AllArgsConstructor to generate All Argument Constructor. Still, it is throwing the error.

After creating All argument constructor manually the error is gone. I did not understand why I need to create a constructor even after mentioning annotations?

Below is my code snippet

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;

#import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Named
@Path("/")
public class HelloEndpoint {
    @Inject
    NamedParameterJdbcTemplate jdbcTemplate;

    @GET
    public String hello() {
        return "Hello World!";
    }

    @Data
    @AllArgsConstructor
    static class Result {
        private final int left;
        private final int right;
        private final long answer;
    }

    // SQL sample
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("calc")
    public Result calc(@QueryParam("left") int left, @QueryParam("right") int right) {
        MapSqlParameterSource source = new MapSqlParameterSource()
                .addValue("left", left)
                .addValue("right", right);
        return jdbcTemplate.queryForObject("SELECT :left + :right AS answer", source,
                (rs, rowNum) -> new Result(left, right, rs.getLong("answer")));
    }
}
1 Answers

There is nothing wrong with Lombok @Data or @AllArgsConstructor.

Please remove the final keywork from your Result class and you will be able to access setter and constructor also.

Related