SpringBoot之请求响应的json数据结构


写在前面: 最近在用SpringBoot搭建一个Blog,由于完全是自己设计并实现的,踩了好多坑。再加上SpringBoot我其实只看了一天,所以处在了一种边百度边写接口的窘境。

请求

  1. 当前端只get请求一个参数时

    1.1 这个参数既可以是实体类,也可以是基本类型,可以不加注解,如下:
    http:127.0.0.1:8080/byTime?page=1&size=1
    注:page和size为类MyPages的两个属性值

    @GetMapping("/byTime")
    public PageInfo<ArticleInfo> listArticleInfoByTime(MyPages myPages){
        return new PageInfo<>(articleService.listArticleInfoByTime(myPages));
    }
    

    1.2 如果这个参数是基本类型的话,也可以:
    http:127.0.0.1:8080/1

    @GetMapping("{id}")
    public PageInfo<ArticleInfo> listArticleInfoByTime(@PathVariale int id){
        return new PageInfo<>(articleService.listArticleInfoByTime(id));
    }
    

    注:@PathVariale 该注解用来解析一个占位符,即把{id}中的值替换为参数值

    也可以加上另外一个注解@RequestParam,用来锁定修改形参值:
    http:127.0.0.1:8080/byTime?id=1

    @GetMapping("/byTime")
    public PageInfo<ArticleInfo> listArticleInfoByTime(@RequestParam("id") int uid){
        return new PageInfo<>(articleService.listArticleInfoByTime(uid));
    }
    
  2. 当前端get请求多个参数时

    2.1 当前端请求一个基本类加实体类时
    http:127.0.0.1:8080/cat?page=1&size=1

    @GetMapping("/{categoryName}")
    public List<ArticleInfo> listArticleInfoByCategory(@PathVariable String categoryName, MyPages pages){
        return articleService.listArticleInfoByCategory(categoryName, pages);
    }
    
  3. 当前端post一个json时,只需要用@RequestBody这个注解即可

        @PostMapping("/update")
    public boolean updateCategoryInfo(@RequestBody CategoryInfo categoryInfo){
        return categoryInfoService.updateCategoryInfo(categoryInfo);
    }
    
  4. 当前端post一个json和一个文件的时候,可以这样

    @PostMapping("/upload")
    public boolean updateCategoryInfo(@RequestBody CategoryInfo categoryInfo,@RequestParam("file") uploadFile)){
        ...
    }
    
  5. 当前端post两个不同的实体类封装为json的时候,可以把它封装为一个Map< Object>
    前端的json格式为:

	    {
       "articleInfo": {
        "title":"第wu次",
        "summary":"第wu次"
    },
    "articleContent":{
    	"content":"第wu次"
    }
}
	    @PostMapping("/post")
    public boolean postArticle(@RequestBody Map<String, Object> map) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();
        String jsonInfo = objectMapper.writeValueAsString(map.get("articleInfo"));
        String jsonContent = objectMapper.writeValueAsString(map.get("articleContent"));
        ArticleInfo articleInfo = objectMapper.readValue(jsonInfo,ArticleInfo.class);
        ArticleContent articleContent = objectMapper.readValue(jsonContent,ArticleContent.class);

        return articleService.insertArticle(articleInfo,articleContent);
    }

响应

  • 大多数情况下,后端给前端返回的都是Json,此时需要加上一个@RestController(注:其实本质还是加的ResponseBody),这个注解的一个功能是把实体类,集合,基本数据类型转为Json格式,如下(ResponseBody即可以注解到类上,也可以注解到方法上):
@RestController
@RequestMapping("/article")
public class ArticleInfoController {

    private final ArticleService articleService;

    @Autowired
    public ArticleInfoController(ArticleService articleService) {
        this.articleService = articleService;
    }
   @GetMapping("/byTra")
    public List<ArticleInfo> listArticleInfoByTra(){
        return articleService.listArticleInfoByTra();
    }
    
}

当返回多个实体类的时候,可以通过List< Object>来封装

    @GetMapping("/content")
    public List<Object> getArticleContent(long id){
        List<Object> list = new ArrayList<>();
        list.add(articleService.getArticleContentById(id));
        list.add(articleService.listCommentOfArticle(id));
        return list;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值