【刨根问底】在Springboot中MVC的常用注解<上>

我们再实际开发过程中一般大致为三层:controller/service/dao或者repository。其中本文最要是分享controller层相关的注解使用。常用的注解有:

  • @RestController

  • @RequestMapping

  • @PathVariable

  • @RequestParam

  • @RequestBody

640?wx_fmt=gif

@RestController

先看源码:

package org.springframework.web.bind.annotation;	
//...无关多余的省略	
@Controller	
@ResponseBody	
public @interface RestController {	
    @AliasFor(annotation = Controller.class)	
    String value() default "";	
}

认真点看,这个注解上有两个曾经使用过的注解,一个@Controller和@ResponseBody。另外网上有人说@RestController是springboot的注解,这里得说清楚一下,并不是这样滴,请看下面:

640?wx_fmt=png

跟springboot半毛钱关系都没有。回到前面,@Controller是标记这个类为controller类,@ResponseBody是将返回数据类型转换为json格式。所以在类上面加注解@RestController表示这个类是controller类并且方法返回参数均为json格式。但是在使用的时候需要注意,如果涉及到页面渲染或页面跳转的不能使用@RestController,只能使用原始的@Controller来处理,所以一般情况下@RestController的使用场景都是前后端分离。一下是一个使用场景:

public class User {	
    private Integer userId;	
    private String userName;	
// set get  ....省略	
}    
@RestController	
public class RestControllerDemoController {	
    @PostMapping("/getUserById")	
    public Object getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}

测试:

640?wx_fmt=png

结果:

640?wx_fmt=png

ok,以上已经成功,我们再回到@ResController换成@Controller将会是什么结果,

@Controller	
public class RestControllerDemoController {	
    @PostMapping("/getUserById")	
    public Object getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}

测试

640?wx_fmt=png

再把代码的方法上加上@ResponseBody。

@Controller	
public class RestControllerDemoController {	
    @PostMapping("/getUserById")	
    @ResponseBody	
    public Object getUserById(Integer id) {	
        return new User(id, "Java后端技术栈");	
    }	
}

再测试:

640?wx_fmt=png

ok,证明了@RestController就是@Controller和@ResponseBody的集合体。

官方对@RestController的解释:

This code uses Spring 4’s new @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It’s shorthand for @Controller and @ResponseBody rolled together.

所以咱们可以这么理解:

@RestController=@Controller+@ResponseBody

既然是两个的集合体,那么我们可以分开去研究,先去看看@Controller。官方解释:

This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning. It is typically used in combination with annotated handler methods based on the RequestMapping annotation.

此注解用作@Component的专用化,允许通过类路径扫描自动检测实现类。它通常与基于请求映射注解的带注解的处理程序方法结合使用。

看@Controller源码:

package org.springframework.stereotype;	
@Target({ElementType.TYPE})	
@Retention(RetentionPolicy.RUNTIME)	
@Documented	
@Component	
public @interface Controller {	
    @AliasFor(annotation = Component.class)	
    String value() default "";	
}

可以看到@Controller注解上有个注解@Component,其余三个注解不是我们本次所关心的,想了解请阅读文章

@Controller相当于dispatcherServlet+Model

640?wx_fmt=png

@Controller同目录下的几个注解,并且@Component也在这个目录下。

1,@Controller所用是 控制器(注入服务):用于标注控制层,相当于struts中的action/controller层,

2,@Service作用是 服务(注入):用于标注服务层,主要用来进行业务的逻辑处理,比如:

@Service("userService")	
public UserServiceImpl implements UserService{	
 //....	
}

,然后再对应需要使用的地方注入,

@Resource("userService")	
private UserService userServie;

3,@repository作用是(实现dao访问):用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件。

4,@Component其中前面三个注解上都有@Component注解, (该注解是把普通pojo实例化到spring容器中,相当于配置文件中的 

<bean id="user" class="com.lawt.domain.User"/>)

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Service等的时候),我们就可以使用@Component来标注这个类。

案例: 

<context:component-scan base-package="com.lawt.*"> 

上面的这个例子是引入Component组件的例子,其中base-package表示为需要扫描的所有子包并且被扫描的类上必须被

@Controller 、@Service、@Repository 、@Component

 注解中的一个注解,都会把这些类纳入进spring容器中进行管理。

后面继续分析剩下的注解,敬请期待!

640?wx_fmt=gif

扫描加qq群获取“干货”,精彩不断!

640?wx_fmt=png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田哥coder

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值