springboot2.0 entry (three) ---- custom programming style

A, RESTFul style API

  1, the advantages:   

  1. ) Look Url to know what resources
  2. ) Look for http method to know what resources
  3. ) See http status code will know the outcome

HTTP methods reflect the operations of resources:

  GET: access to resources
  POST: Add a resource
  PUT: Modify resource
  DELETE: Delete Resource

Second, the code shows:

/**
 * @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;
}

New Animal class, using annotations that contain settings get / set methods, all the parameters constructor, no-argument constructor, builder to quickly create objects

/**
 * @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);
    }

}

 

New AnimalController, do test postMan:

 

 

 Create a post request (add), returns the created object;

@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

 

Guess you like

Origin www.cnblogs.com/liweiweicode/p/11622313.html