Can @Service Replace @Controller in Spring Boot? A Deep Dive

This article explores whether the @Service annotation can replace @Controller in Spring Boot, demonstrates a working example, explains the underlying bean registration process, and clarifies how Spring’s component scanning and request mapping enable such unconventional usage.

macrozheng
macrozheng
macrozheng
Can @Service Replace @Controller in Spring Boot? A Deep Dive

Preface

In Spring Boot development, @Controller marks a controller class that handles web requests, while @Service marks a service class responsible for business logic. This article investigates whether a class annotated with @Service can be used to handle controller responsibilities.

Using @Service Instead of @Controller

We define a ServiceController class annotated with @Service and @RequestMapping("/ts"). The class autowires a UserMapper to interact with the data layer and exposes a getServices method via @GetMapping("get-services"):

@Service
@RequestMapping("/ts")
public class ServiceController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("get-services")
    @ResponseBody
    public User getServices() {
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getUsername, "zhangSan"));
        return user;
    }
}

Sending a GET request to http://localhost:8080/ts/get-services with PostMan returns the expected JSON response, confirming that the @Service -annotated class can handle HTTP requests just like a traditional controller.

Underlying Mechanism

Both @Controller and @Service are detected by Spring’s component scanning, which is triggered by the @SpringBootApplication meta‑annotation (composed of @EnableAutoConfiguration, @ComponentScan, and @Configuration). Classes annotated with either stereotype that reside in the main application package or its sub‑packages are registered as beans in the Spring container.

During startup, ConfigurationClassPostProcessor processes bean definitions, and AbstractHandlerMethodMapping later maps URLs to methods based on @RequestMapping (or its shortcut annotations). As long as the class is a bean and carries request‑mapping annotations, Spring MVC can treat it as a controller, regardless of whether it is marked with @Controller or @Service.

Summary

In Spring Boot, @Controller and @Service are both component stereotypes that can be discovered and registered by the container. When a @Service -annotated class also carries @RequestMapping (or related) annotations, Spring MVC will route HTTP requests to its methods, effectively allowing @Service to function as a controller. This behavior stems from Spring’s component scanning, bean registration, and the request‑mapping mechanism within AbstractHandlerMethodMapping.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaannotationsSpringBootControllerService
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.