Is it possible to proxy a POJO in microservices application?

Viewed 1106

I would like to avoid duplicating my POJOs in a microservices application, so I am wondering if there is a way to do that (like proxying)?

I mean, is there a way for a Service A to access POJOs (or other classes/interfaces) defined inside a Service B without physically creating these POJOs classe files in Service A?

The big big challenge in a microservice architecture is that point and I didn't find a way to solve it.

2 Answers

Follow this rule of thumb:

For enterprise applications and complex objects, create a 3'rd project (an API project), and share it among your services as a dependency.

For simple and self-descriptive objects, use 'copies' of the same objects in each one of your services; note that this is powerful since the POJOs don't need to be identical;

For instance:

In one service (A), it can look like this:

@Entity
public class ExchangeValue {

    @Id
    private Long id;

    @Column(name = "currency_from")
    private String from;

    @Column(name = "currency_to")
    private String to;

    @Column(name = "conversion_multiple")
    private BigDecimal conversionMultiple;
    ...
}

wheres in another service (B) it can be much shorter, and with different types:

public class ExchangeValue {
    private int conversionMultiple;
    ...
}
Related