I've been trying to solve it for a while but I couldn't find anything to help me, in my Spring Boot Project, in one of the assembler classes I extend "RepresentationModelAssemblerSupport", when I extend the class I start to have a conflict with my Lombok @AllArgsContructor
, I use it to not use @Autowired
keeping the code clean, so I can't compile, and I get the following Warning in the annotation "Lombok needs a default constructor in the base class", but I already have a constructor in the Class
After a lot of research, I saw that in some cases other annotations like @NoArgsConstructor
and @RequiredArgsConstructor
helped but I was not successful.
@Component
@AllArgsConstructor
public class SaleModelAssembler
extends RepresentationModelAssemblerSupport<Sale, SaleModel> {
@Autowired
private ModelMapper modelMapper;
public SaleModelAssembler() {
super(SaleResource.class, SaleModel.class);
}
@Override
public SaleModel toModel(Sale sale) {
SaleModel saleModel = createModelWithId(sale.getCode(), sale);
modelMapper.map(sale, saleModel);
saleModel.getItems().forEach(saleItemModel -> {
saleItemModel.getProduct().add(linkTo(ProductResource.class)
.slash(saleItemModel.getProduct().getId()).withSelfRel());
});
return saleModel;
}
@Override
public CollectionModel<SaleModel> toCollectionModel(Iterable<? extends Sale> entities) {
return super.toCollectionModel(entities).add(linkTo(SaleResource.class).withSelfRel());
}
}
Lombok can't call the super constructor of a class (how to Call super constructor in Lombok), so the annotations you are using can't do the job.
If you want not to use the @Autowired
annotation, you can try this:
@Component
public class SaleModelAssembler extends RepresentationModelAssemblerSupport<Sale, SaleModel> {
private ModelMapper modelMapper;
public SaleModelAssembler(ModelMapper modelMapper) {
super(SaleResource.class, SaleModel.class);
this.modelMapper = modelMapper;
}