Adrian Lagartera
Adrian Lagartera

Reputation: 506

Can parse a projection interface to object? Spring

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.

Upvotes: 4

Views: 2645

Answers (1)

Crakiz
Crakiz

Reputation: 56

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.

Upvotes: 4

Related Questions