一、RESTFul风格API
1、优点:
- )看Url就知道要什么资源
- )看http method就知道针对资源干什么
- )看http status code就知道结果如何
HTTP方法体现对资源的操作:
GET : 获取资源
POST : 添加资源
PUT : 修改资源
DELETE : 删除资源
二、代码演示:
/**
* @author Levi
* @date 2019/9/18 9:31
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Animal {
private String name;
private Integer type;
private String num;
private Long id;
private Date birthDate;
}
新建Animal类,使用注解,包含设置get/set方法、全部参数构造器、无参数构造器、builder快速创建对象
/**
* @author Levi
* @date 2019/9/18 9:36
*/
@Slf4j
@RestController
@RequestMapping("/rest")
public class AnimalController {
@RequestMapping(value = "/animals", method = POST, produces = "application/json")
public AjaxResponse saveArticle(@RequestBody Animal animal) {
log.info("saveArticle:{}",animal);
return AjaxResponse.success(animal);
}
@RequestMapping(value = "/animals/{id}", method = DELETE, produces = "application/json")
public AjaxResponse deleteArticle(@PathVariable Long id) {
log.info("deleteAnimals:{}",id);
return AjaxResponse.success(id);
}
@RequestMapping(value = "/animals/{id}", method = PUT, produces = "application/json")
public AjaxResponse updateArticle(@PathVariable Long id, @RequestBody Animal animal) {
animal.setId(id);
log.info("updateArticle:{}",animal);
return AjaxResponse.success(animal);
}
@RequestMapping(value = "/animals/{id}", method = GET, produces = "application/json")
public AjaxResponse getArticle(@PathVariable Long id) {
Animal animal = Animal.builder().id(1L).name("levi").build();
return AjaxResponse.success(animal);
}
}
新建 AnimalController,用postMan做测试:
新建一个post请求(添加),返回创建的对象;
@RestController = @Controller + @ResponseBody
@Slf4j
@Controller
@RequestMapping("/rest")
public class AnimalController {
@RequestMapping(value = "/animals", method = POST, produces = "application/json")
public @ResponseBody AjaxResponse saveArticle(@RequestBody Animal animal) {
log.info("saveArticle:{}",animal);
return AjaxResponse.success(animal);
}
上述注解可以改为上面的代码所示
@PathVariable 参数说明
delete请求例子
三、json配置:
springboot默认json工具为:jackjson
各种json工具性能对比:https://blog.csdn.net/accountwcx/article/details/50252657
@JsonIgnore 排除属性不做序列化与反序列化
@JsonProperty 为属性换一个名
@JsonPropertyOrder(value={"pname1","pname2"}) 改变json子属性的默认定义的顺序
@JsonInclude(JsonInclude.Include.NON_NULL) 排除为空的元素不做序列化反序列化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 指定属性格式
全局时间配,在yml文件中配置,避免在请求时间的时候,格式不一致报错,
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
实际代码演示:
无法识别时间格式:
yml中添加配置,重启项目(一般修改配置文件需要重启项目,否则热部署一般是可以使用的)
没中间的T报错,有就不报错(无解)
这样不报错
@JsonIgnore 排除属性不做序列化与反序列化
@JsonProperty 为属性换一个名
@JsonPropertyOrder(value={"pname1","pname2"}) 改变json子属性的默认定义的顺序
@JsonInclude(JsonInclude.Include.NON_NULL) 排除为空的元素不做序列化反序列化
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 指定属性格式
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@JsonPropertyOrder(value={"id","type"})//更换变量显示顺序
public class Animal {
@JsonIgnore //不支持序列化,不能传参
private String name;
@JsonProperty("type1")
private Integer type;
//空值不返回
@JsonInclude(JsonInclude.Include.NON_NULL)
private String num;
private Long id;
private Date birthDate;
List<Animal> animalList;
private Animal animal2;
}
java实体类:
请求参数
返回值