尚硅谷B2C电商毕设微服务项目中所用注释总结

周青的总结记录


前言

上次自己手写spring项目已经过去了很久,本次学习尚硅谷B2C电商毕设项目时遇到很多注释,虽然大概感觉得到他是做什么的,但不能具体地说出他的作用。现在在本文中对遇到的注释进行总结。以及记录因对这些注释不熟悉而在开发中出现的错误。


正文

仅作粗略记录,详解请参考其他文章的具体解读

通用服务 commons

@Data

出现在pojo,param下的实体类中,省去代大量的get()、 set()、 toString()等方法

@TableName

出现在pojo下的实体类中,对应数据库中的表名,实现实体类型和数据库中的表实现映射

@TableId

出现在pojo下的实体类中,加在实体中表示主键的属性上。
作用:用于设置id的生成策略,即设置id是如何生成的
type 设置主键类型,AUTO:数据库自增。
i.e. :

@TableId(type = IdType.AUTO)

@TableField

”对应j数据库属性名“(赵伟风老师原话)
在MP中通过@TableField注解可以指定字段的一些属性,常常解决的问题有两个:
1.对象中的属性名和表中的字段名不一致(非驼峰)
2.对象中的属性字段在表中不存在

@JsonProperty

出现在pojo,param下的实体类中,”对应json格式化名称“(赵伟风老师原话)给属性重命名(多一个名字来识别)
在postman测试中传参数名需要传@JsonProperty中的名称
i.e.
实体类为

    @NotNull
    @JsonProperty("user_id")
    private Integer userId;

则测试的json应为”user_id"而不是"userId"
如果有[nio-3001-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: @NotNull method com/atguigu/param/AddressListParam.getUserId must not return null] with root cause的JSON转换错误可能原因正在于此。

@NotNull

非空校验。一般用于 Integer 类型的基本数据类型

@NotBlank

非空校验。只能用于 String 类型。要和 @valid 一起使用

@Length

一般用在 String 类型上可对字段数值进行最大长度限制的控制
i.e. :

@Length(min = 6)

@JsonInclude()

为实体类序列化返回增加规则。
i.e. @JsonInclude(Include.NON_NULL):当此值不为空时生成json,过滤掉返回值为null的字段,即字段为null不参与序列化。

@FeignClient

@FeignClient声明Feign的客户端,指明服务名称

【Feign是一个http请求调用的轻量级框架,用于完成服务间远程调用及负载均衡】
Feign会通过动态代理,帮我们生成实现类。
注解@FeignClient声明Feign的客户端,指明服务名称
接口定义的方法,采用SpringMVC的注解。Feign会根据注解帮我们生成URL地址
i.e. :

@FeignClient(value = "provider-service")
public interface ConsumerService {
    
    
    
    //String url = String.format("http://provider-service/user/findUserById/%s",id);
    @RequestMapping("/user/findUserById/{id}")
    User findUserById(@PathVariable("id") Integer id);
}

@GetMapping

【@RequestMapping作用:将请求和处理请求的控制器方法关联起来,建立映射关系】
在SpringMVC新版本中,@RequestMapping被
@GetMapping
@PostMapping
@PutMapping
等替代。
@GetMapping用于处理请求方法的GET类型

@PathVariable

@PathVariable 映射 URL 绑定的占位符
通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx”) 绑定到操作方法的入参中。
i.e.见上

@AllArgsConstructor @NoArgsConstructor

出现在pojo下的实体类中,
@AllArgsConstructor 有参构造函数
@NoArgsConstructor 无参构造函数
常与@Bulider连用
如果在测试接口时,postman提示400,后台报错提示.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.atguigu.pojo.xxx(although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance ofcom.atguigu.pojo.xxx (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 2, column: 5]]
错因可能为实体类中缺失这个注释。


业务服务 store

@SpringBootApplication

出现在Application启动类中,springboot的核心注释,run整个框架。

@MapperScan

出现在Application启动类中,指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
(比一个个加@Mapper注解更简便)

@RestController

出现在controller下的Controller类中
@RestController = @Controller + @ResponseBody
在一个类上添加@Controller注解,表明了这个类是一个控制器类。
@ResponseBody表示方法的返回值直接以指定的格式写入Http response body中。

@RequestMapping

将请求和处理请求的控制器方法关联起来,建立映射关系。
i.e.:

@Controller
@RequestMapping("/demo")
public class DemoController {
    
    
    @RequestMapping("/demo1")
    public String toDemo(){
    
    
        return "demo";
    }
}

此时,servlet的路径是http://localhost:端口号/项目名称/demo/demo1

@Autowired

出现在controller下的Controller类,impl类中
注解@Autowired是Spring对组件自动装配的一种方式。常用于在一个组件中引入其他组件。
自动装配:sprng通过依赖注入(DI),完成IOC容器中各个组件依赖的关系赋值。

@PostMapping

出现在controller下的Controller类中
见上文@GetMapping@RequestMapping

@Slf4j

日志输出


猜你喜欢

转载自blog.csdn.net/two_brother_/article/details/128953653