Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Data Is Not Creating Constructor For Static Nested Class

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")));
    }
}
like image 915
Tannu Avatar asked Jun 20 '26 07:06

Tannu


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.

like image 133
Yogesh Prajapati Avatar answered Jun 22 '26 20:06

Yogesh Prajapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!