Home » Java » java – Can parse a projection interface to object? Spring-Exceptionshub

java – Can parse a projection interface to object? Spring-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Actually have a projection interface in Spring. Like this:

public interface DaoObjectProjection{
    Integer getTotalAmount();
    String getCode();
    String getName();
    String getLastName();
}

And i want send this to another microservice, i know cant send the interface because the proxy funcionality dont work if use RestTemplate. For thar reason i use an another Object like that:

public class ObjectWantSend {
    private Integer totalAmount;
    private String code;
    private String name;
    private String lastName;

//Getters
//Setters

}

My question if exist any way to parse directly my projection interface into this object or need setting one by one like this:

ObjectWantSend.setTotalAmount(DaoObjectProjection.getTotalAmount);

I am using Hibernate with Spring.

How to&Answers:

I think, like other user says, you should use MapStruct. This could be a solution for your problem:

    @Mapper
    public interface ObjectMapper {

        ObjectMapper INSTANCE = Mappers.getMapper( ObjectMapper.class );

        @Mapping(source = "totalAmount", target = "totalAmount")
        ObjectWantSend objectWantSend(DaoObjectProjection aux);
    }

That library works so good with Spring and Hibernate. I hope you find it usefull.