In a REST service project, where should we put code that calls a third party REST API?

  softwareengineering

My project is organized in layers like

  • Configuration
  • Controller
  • Service
  • Repository
  • Domain

Our REST Controller is in the Controller layer. We need to connect to another API via REST calls and I’m not sure if I should make a new Controller or do it in the Service layer.

I’m going to implement the REST calls with Spring’s RestTemplate.

The way our API will be used is like this:

  • REST call is made that goes through our REST endpoint.
  • Our REST endpoint will call the third party API (either by calling another controller method or a service method).
  • We will then do a conversion between our format and the third party format
  • Then we will return the JSON of our format back to the client

I would put it into the Service layer. It doesn’t make sense for you to do it in a NEW Controller. You can even do the remote call directly in your controller if you are sure that other controllers don’t need to do a similar thing.

1

You should call the third party in your Service layer. The reason being, the Service layer is typically used for all aggregations and related logic and from your use case you can call to third party API and perform subsequent transformation from their format to yours in the Service layer.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

In a REST service project, where should we put code that calls a third party REST API?

My project is organized in layers like

  • Configuration
  • Controller
  • Service
  • Repository
  • Domain

Our REST Controller is in the Controller layer. We need to connect to another API via REST calls and I’m not sure if I should make a new Controller or do it in the Service layer.

I’m going to implement the REST calls with Spring’s RestTemplate.

The way our API will be used is like this:

  • REST call is made that goes through our REST endpoint.
  • Our REST endpoint will call the third party API (either by calling another controller method or a service method).
  • We will then do a conversion between our format and the third party format
  • Then we will return the JSON of our format back to the client

I would put it into the Service layer. It doesn’t make sense for you to do it in a NEW Controller. You can even do the remote call directly in your controller if you are sure that other controllers don’t need to do a similar thing.

1

You should call the third party in your Service layer. The reason being, the Service layer is typically used for all aggregations and related logic and from your use case you can call to third party API and perform subsequent transformation from their format to yours in the Service layer.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT