学到哪个不太熟悉的就把哪个新增上,自己学习备用。
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处理方法上。
@PostMapping也是一个组合注解,是@RequestMapping(method = RequestMethod.POST)的缩写。
@RequestMapping如果没有指定说明请求方式,将接受get、post、head、options等所有的请求方式。
@RestController注解相当于@ResponseBody + @Controller合在一起的作用。返回json类型数据。
@Resource的作用相当于@Autowired,只不过@Autowired按照byType自动注入。注入时使用,不用在写get、set方法。
@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的 spring配置类,可以用来替代相应的xml配置文件。
@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,通常会自动根据你的类路径和你的bean定义自动配置。
@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,当然@Compone下的子注解@Service,@Repository,@Controller。
@EnableDiscoveryClient与@EnableEurekaClient 用法上基本一致,区别:
@EnableDiscoveryClient基于spring-cloud-commons, @EnableEurekaClient基于spring-cloud-netflix。
如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用
@EnableDiscoveryClient。
@ConfigurationProperties
在application.properties 中配置,加在类上,需要和@Component注解,结合使用.代码如下:
@Component
@ConfigurationProperties(prefix = “com.example.demo”)
通过@Bean的方式进行声明,这里我们加在启动类即可,代码如下:
@SpringBootApplication
public class DemoApplication {
@Bean
@ConfigurationProperties(prefix = “com.example.demo”)
public People people() {
return new People();
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@JsonIgnoreProperties(ignoreUnknown = true),将这个注解写在类上之后,就会忽略类中不存在的字段,可以满足当前的需要。这个注解还可以指定要忽略的字段。使用方法如下:
@JsonIgnoreProperties({ “internalId”, “secretKey” })
指定的字段不会被序列化和反序列化。
@GeneratedValue(strategy=GenerationType.IDENINY)
PS:@GeneratedValue注解的strategy属性提供四种值:
-AUTO主键由程序控制, 是默认选项 ,不设置就是这个
-IDENTITY 主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式
-SEQUENCE 通过数据库的序列产生主键, MYSQL 不支持
-Table 提供特定的数据库产生主键, 该方式更有利于数据库的移植
@Id 标注用于声明一个实体类的属性映射为数据库的主键列
**@ApiModelProperty()**用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏
比如:
@ApiModelProperty(value=“用户名”,name=“username”,example=“xingguo”)
private String username;
@DateTimeFormat(pattern=“yyyy-MM-dd”)
@JsonFormat(timezone = “GMT+8”, pattern = “yyyy-MM-dd HH:mm”) 时差8小时,必须加
@Transient 详见https://blog.csdn.net/OrangQceee/article/details/80446441
1.@ApiParam 顾名思义,是注解api的参数,也就是用于swagger提供开发者文档,文档中生成的注释内容。
@ApiOperation( value = "编辑公告", notes = "编辑公告", httpMethod = "POST" )
@RequestMapping( value = "/edit", method = RequestMethod.POST )
public RequestResult edit(
@ApiParam(name = "title", value = "公告标题", required = true) @RequestParam("title") String title,
@ApiParam(name = "content", value = "公告内容", required = true) @RequestParam("content") String content){
2.@RequestParam,是获取前端传递给后端的参数,可以是get方式,也可以是post方式。其中如果前端传递的参数和后端你接受的参数起的名字字段是一致的可以省略不写,也可以直接写@RequestParam String title,如果不一致一定要完整写,不然获取不到,如下面的bis_key就必须写。
@ApiOperation( value = "编辑公告", notes = "编辑公告", httpMethod = "POST" )
@RequestMapping( value = "/edit", method = RequestMethod.POST )
public RequestResult edit(
@ApiParam(name = "bis_key", value = "bis_key", required = true) String bisKey,
@ApiParam(name = "title", value = "公告标题", required = true) @RequestParam String title,
@ApiParam(name = "content", value = "公告内容", required = true) String content,
3.@PathVariable,是获取get方式,url后面参数,进行参数绑定
@ApiOperation(value = "删除公告", notes = "删除公告", httpMethod = "POST")
@RequestMapping(value = "/delete/{bisKey}", method = RequestMethod.POST)
public RequestResult remove(@ApiParam(name = "bisKey", value = "需要删除的公告ids", required = true) @PathVariable String bisKey) {
@RequestParam适用于name-valueString类型的请求域,@RequestPart适用于复杂的请求域(像JSON,XML)。