Spring/Java project hierarchy: Where to put none model POJO classes?

Viewed 497

I have a @Service class FooService that queries some external API. In return, it gets some FooResponse. This FooResponse is being used to create the model object but it's not a model by itself.

The project has a typical Spring project hierarchy (i.e. service, model, controller, config, utils)

Where should FooResponse reside at? Is it a dto?

Would you go with dto.foo.FooResponse.java? Or maybe just dto.FooResponse.java?

1 Answers

If you can define your architecture using hexagonal architecture then you should define domain model called Foo in domain/application layer and FooService should return Foo domain model and FooResponse can be encapsulated in FooServiceImpl

Package Structure

com..application.model.Foo
com..application.service.FooService
com..infrastructure.service.impl.FooServiceIml

interface FooService

Foo getFoo(T someParameter)

Class FooServiceImpl

com..infrastructure.service.impl
public class FooserviceImpl {

public Foo getFoo(T someParameter) {
  // call external service
  // getFooResponse
  // Map FooResponse to Foo and return Foo
}

In hexagonal architecture application/domain layer does not depend on outer layer (presentation, persistence, infrastructure). The benefit of this architecture is that you can change infrastructure code without affecting business logic(for example FooServiceImpl can be changed to use gRpc instead of REST without any change in application logic)

Related