Actframework中如何灵活控制JSON响应

@DiamondFsd 昨天写了一篇关于如何在Spring MVC中灵活控制JSON返回的博客,其中@JSON注解的思路和我在actframework的处理方式很是类似。前端

恰好昨天把actframework放上码云了,就这个话题写下 如何在ActFramework中控制JSON返回java

首先是资源和服务。我将就@DiamondFsd的article的例子来作讲解git

@Entity("article")
public class Article extends MorphiaAdaptiveRecord<Article> {

    @UrlContext("article")
    public static class Service extends MorphiaDao<Article> {

        @GetAction
        public Iterable<Article> list() {
            return findAll();
        }

        @GetAction("{id}")
        public Article show(String id) {
            return findById(id);
        }

        @PostAction
        public Article create(Article article) {
            return save(article);
        }
        ...
    }

}

这里咱们看到能够经过 POST /article向服务提交article数据。假设我提交的数据是:json

{
	"title": "How to control JSON view in Actframework",
	"content": "BlahBlah",
	"author": "Gelin Luo",
	"language": "Java",
	"framework": "Actframework",
	"tags" : [
		{"name": "java"},
		{"name": "mvc"},
		{"name": "json"}
	]
}

我能够获得相似下面的返回:后端

{
  "id": "58a6409ab6c6fe2138b67f10",
  "_created": "17/02/2017 11:15:22 AM",
  "content": "BlahBlah",
  "v": 1,
  "language": "Java",
  "author": "Gelin Luo",
  "title": "How to control JSON view in Actframework",
  "_modified": "17/02/2017 11:15:22 AM",
  "framework": "Actframework",
  "tags": [
    {
      "name": "java"
    },
    {
      "name": "mvc"
    },
    {
      "name": "json"
    }
  ]
}

当我发出GET /article请求时,Article.Service.list()方法会响应并返回全部的article列表:mvc

[
  {
    "id": "58a6409ab6c6fe2138b67f10",
    "_created": "17/02/2017 11:15:22 AM",
    "content": "BlahBlah",
    "v": 1,
    "language": "Java",
    "author": "Gelin Luo",
    "title": "How to control JSON view in Actframework",
    "_modified": "17/02/2017 11:15:22 AM",
    "framework": "Actframework",
    "tags": [
      {
        "name": "java"
      },
      {
        "name": "mvc"
      },
      {
        "name": "json"
      }
    ]
  }
]

那若是我想控制返回列表的数据,让每项只返回authortitle,我能够在list()方法上面添加注解PropertySpec.net

@GetAction
@act.util.PropertySpec("author,title")
public Iterable<Article> list() {
    return findAll();
}

而后再发出GET /article请求,就能够获得下面的响应了:code

[
  {
    "author": "Gelin Luo",
    "title": "How to control JSON view in Actframework"
  }
]

我能够在Article.Service.show(String)方法上采用相似的方法来定义须要返回的字段。有人提到过若是想让前端向后端在请求中传递须要的字段该怎么办,下面是Actframework提供的方法:blog

show(String)方法作一点改动资源

@GetAction("{id}")
public Article show(String id) {
    return findById(id);
}

变为

@GetAction("{id}")
public Article show(String id, String fields) {
    PropertySpec.current.set(fields);
    return findById(id);
}

而后就能够从前端在请求中加载fields参数了:

GET /article/58a6409ab6c6fe2138b67f10?fields=-tags,-content,-_created

上面的请求表示从返回JSON结果中去掉tags, content,和_created三个字段

返回结果将会是:

{
  "id": "58a6409ab6c6fe2138b67f10",
  "v": 1,
  "language": "Java",
  "author": "Gelin Luo",
  "title": "How to control JSON view in Actframework",
  "_modified": "17/02/2017 11:15:22 AM",
  "framework": "Actframework"
}

该博客的完整源码在码云

ActFramework: https://www.oschina.net/p/actframework

相关文章
相关标签/搜索