SpringBoot入门实现RESTFUL API以及用Postman测试

Model

@Data
@Builder
public class Article {
    private Long id;
    private String author;
    private String title;
    private String content;
    private Date createTime;
}

contral 

@Slf4j
@RestController
@RequestMapping("/rest")
public class ArticleController {

    // 查询一篇文章
   //@RequestMapping(value = "/articles/{id}", method = RequestMethod.GET)
    @GetMapping(value = "/articles/{id}")
    public AjaxResponse getArticle(@PathVariable("id") Long id){
        Article article = Article.builder()
                .id(1L)
                .author("xieyang")
                .content("spring boot 从青铜到王者")
                .createTime(new Date())
                .title("t1").build();

        log.info("article:" + article);
        return AjaxResponse.success(article);
    }

    // 新增一篇文章
    //@RequestMapping(value = "/articles", method = RequestMethod.POST)
    @PostMapping(value = "/articles")
    public AjaxResponse saveArticle(@RequestBody Article article){
        log.info("saveArticle:" + article);
        return AjaxResponse.success();
    }

    // 修改一篇文章
    //@RequestMapping(value = "/articles", method = RequestMethod.PUT)
    @PutMapping(value = "/articles")
    public AjaxResponse updateArticle(@RequestBody Article article){
        log.info("updateArticle:" + article);
        return AjaxResponse.success();
    }

    // 删除一篇文章, 根据id
    //@RequestMapping(value = "/articles", method = RequestMethod.DELETE)
    @DeleteMapping(value = "/articles")
    public AjaxResponse deleteArticle(@PathVariable("id") Long id){
        log.info("updateArticle:" + id);
        return AjaxResponse.success();
    }
}

测试查询文章接口

SpringBoot入门实现RESTFUL API以及用Postman测试_第1张图片

 测试上传文章接口

SpringBoot入门实现RESTFUL API以及用Postman测试_第2张图片

日志显示报错,因为没有处理日期。

在配置文件中添加如下配置

server:
  port: 8888

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

 再来测试一次

SpringBoot入门实现RESTFUL API以及用Postman测试_第3张图片

测试put和delete接口

SpringBoot入门实现RESTFUL API以及用Postman测试_第4张图片 Delete接口,测试成功

SpringBoot入门实现RESTFUL API以及用Postman测试_第5张图片

你可能感兴趣的:(算法)