最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Which controller for Spring Boot MVC? - Stack Overflow

programmeradmin2浏览0评论

For a normal MVC controller which annotation do I want? Both are on the classpath.

org.springframework.stereotype.Controller;
org.springframework.web.servlet.mvc.Controller;

I'm using

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.2</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>6.2.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

For a normal MVC controller which annotation do I want? Both are on the classpath.

org.springframework.stereotype.Controller;
org.springframework.web.servlet.mvc.Controller;

I'm using

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.2</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>6.2.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Share Improve this question asked Feb 6 at 9:19 StewartStewart 18.3k8 gold badges55 silver badges84 bronze badges 2
  • 1 org.springframework.stereotype.Controller is an annotation, org.springframework.web.servlet.mvc.Controller is an interface. Ditch the spring-framework-bom as Spring Boot already manages your Spring version, this setup you now have will complicate upgrading. – M. Deinum Commented Feb 6 at 9:23
  • @M.Deinum Thank you. If you post as an answer, I will accept. – Stewart Commented Feb 6 at 9:24
Add a comment  | 

1 Answer 1

Reset to default 2
  • org.springframework.stereotype.Controller is an annotation
  • org.springframework.web.servlet.mvc.Controller is an interface.

Hence there is no choice to make as there is only 1 annotation for this (not including @RestController which is a further specialization of the @Controller for rest based controllers).

PRO-TIP: Ditch the spring-framework-bom as Spring Boot already manages your Spring version, this setup you now have will complicate upgrading.

发布评论

评论列表(0)

  1. 暂无评论