0%

DEMO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.samjava.mall.controller;

import com.samjava.mall.AjaxResponse;
import com.samjava.mall.model.Article;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/rest") //请求前缀。
public class ArticleController {

//查询一篇文章,根据ID
// @RequestMapping(value = "/articles/{id}",method = RequestMethod.GET)
@GetMapping("/articles/{id}")
public AjaxResponse getArticle(@PathVariable("id") Long id){

Article article = Article.builder()
.id(1L)
.author("Sam")
.content("内容内容")
.createTime(new Date())
.title("tittle1").build();
log.info("getArticle:"+article);
return AjaxResponse.success(article);
}


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

return AjaxResponse.success();
}


//修改一篇文章
// @RequestMapping(value = "/articles",method = RequestMethod.PUT)
@PutMapping("/articles")
public AjaxResponse updateArticle(@RequestBody Article article){

// if(article.getId() == null){
// //
// }

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

//删除一篇文章
// @RequestMapping(value = "/articles/{id}",method = RequestMethod.DELETE)
@DeleteMapping("/articles/{id}")
public AjaxResponse deleteArticle(@PathVariable("id") Long id){

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

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.samjava.mall;
import lombok.Data;

@Data
public class AjaxResponse {
private boolean isok;
private int code;
private String message;
private Object data;


public AjaxResponse(){}

public static AjaxResponse success () {
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse
.setMessage("delete success ");
return ajaxResponse;
}


public static AjaxResponse success (Object obj) {
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse
.setMessage("query success ");
ajaxResponse.setData(obj);
return ajaxResponse;
}


public static AjaxResponse success (Object obj,String msg ) {
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse
.setMessage(msg);
ajaxResponse.setData(obj);
return ajaxResponse;
}
}

yml

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

http协议与Spring参数接收注解

Spring请求响应处理流程

ResponseBody & RequestBody