SpringCloud项目常用注解

各层级常用@注解集合:

1. Controller层:

@Slf4j
@RestController
@RequestMapping(value = "/***")
public class MyController {
	/**
		@Autowired
		@Resource
		@GetMapping
		@PostMapping
		@RequestMapping
	**/
	//Controller methods
	 @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
	 public Result<List<User>> getUserList(@RequestParam("userId") @Validated Long userId) {
			//...
	}
	 @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
	 public Result<List<User>> getUserList(@RequestBody @Validated User user) {
			//...
	}

}

实体类:

@Data
public class User {

	/**
	 * 用户id
	 */
    @NotNull(message = "用户id不能为空")
    private Long userId;

    /**
     * 开始日期
     */
    @NotNull(message = "开始时间不能为空")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    /**
     * carIds集合
     */
    @NotEmpty(message = "carIds不能为空")
    private List<Long> carIds;
	
	/**
     * 不需要进行Json返回的注释  -- 常用于VO
     */
    @JSONField(serialize = false)
    private Integer test;

2.Service层:

@Service
@Slf4j

3.Dao层:

@Repository
@Param

4. 其它

4.1 Spring Boot 启动类

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class MySpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MySpringBootApplication.class, args);
    }
}

4.2 FeignService

@FeignClient(name = "***-***")//服务名
public interface IXxxFeignService{
	//接口
}

5. 注解详情介绍

5.1 Spring中 @Param 和 @RequestParam的使用区别?

@RequestParam 用于controller层
(1)解决前台参数名称与后台接收参数变量名称不一致的问题,等价于request.getParam
(2)可设置value:指定参数名 default:指定变量初始值 require(true默认/false):指定参数是否为必传
@Param 用于dao层
个人理解为修饰参数,使得mapper.xml中的参数与后台的参数对应上,也增强了可读性
如果两者参数名一致得话,spring会自动进行封装,不一致的时候就需要手动去使其对应上。
@Param是mybatis中的注解,用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 。

5.2 @Controller 和@RestController的区别?

知识点:@RestController注解相当于@ResponseBody + @Controller合在一起的作用。

  1. 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
  2. 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值