gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
call service in main spring boot
- User
call service in main spring boot
- Assistant
To call a service in a main Spring Boot application, you can follow these steps:
- Create a service class with the required business logic.
@Service public class MyService { public String getData() { // business logic return "Data from service"; } }- Inject the service into the main application class.
@SpringBootApplication public class MyApp { @Autowired private MyService myService; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @GetMapping("/data") public String getData() { return myService.getData(); } }-
Use the injected service in the main application class using the
@Autowiredannotation. -
You can then use the service in any of the methods in the main application class, such as in a controller method.
In the example above, the
MyServiceclass is injected into theMyAppclass, and thegetData()method is called in thegetData()endpoint method. You can then run your application and call the endpoint to get the data from the service.