-3

First a disclaimer – I am very new to programming. This project is for what's more or less a capstone, and we're working in the Spring Boot framework (Java language, Thymeleaf views).

The overall gist of my program is this: a user fills out a form, and those parameters are used to make a call to the Yelp Fusion API.

I spent a lot of time figuring out how to make the API call, and sort of neglected to think about where to do it. It's currently in my Controller class, but now I'm not so sure if that's the best idea – maybe it should go in a Model or even a DTO? I'd appreciate any insight, as the general internet seems to be conflicted.

Here's the link to the repository on GitHub if that helps.

Thanks!

  • Hi & Welcome to [so]! *How?* (since you have springboot-sterter-web in stack) restTemplate! (google, baeldung,...synchronous!) *Where?* for simplicity reasons you could do it in the controller (`@Autowired RestTemplate ...` and "fire"!)... in a finer architecture you would have some separation (service layer...) – xerx593 May 17 '21 at 23:56
  • and "web-client", if you want to do it asynchronously/reactive (https://www.baeldung.com/spring-webclient-resttemplate, ...) – xerx593 May 18 '21 at 00:01
  • Generally speaking, the responsibility of a Web controller is _specifically_ to serve as an interface between the outward-facing HTTP API and the internals of your application, which should be expressed in business terms (usually as interfaces and implementing classes). You would have something like an `interface SearchService` (which you inject into your controller via a constructor parameter) and a `class YelpFusionSearch implements SearchService`. – chrylis -cautiouslyoptimistic- May 18 '21 at 01:37

1 Answers1

0

It is preferred to make any client calls or write business logic for the API in the Service layer.

In your case, you can create a service package and create some interface YelpClientCallService.java which is autowired in your controller, and a class YelpClientCallServiceImpl.java that implements the YelpClientCallService.java interface. Now, you can call the client API from this class and then perform business logic on the response received, or you can just return the response to the controller.

You can also annotate the service class as @Service instead of @Component. It is just a special name for @Component and will help in classification. For more details on package structure in Springboot you can check the below resources.

  1. Spring docs : this has the basic package structure that is followed.
  2. This medium blog is also decent to understand other aspects of project structure and best practices.