为使用操作数JSON的搜索构建REST api的最佳方法 [英] Best way to build REST api for a search with operands JSON

查看:157
本文介绍了为使用操作数JSON的搜索构建REST api的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序构建REST api.基本操作已完成.但是现在我需要一些帮助来搜索不同的参数及其操作数.

I'm trying to build a REST api for my application. The basics are done. But now I need some help to for a search with different parameters and their operands.

假设我有一个像这样的JSON字符串:

Let's say I have a JSON String like this:

{"v00Datvon":"09-07-2014","v00Datbis":"09-07-2014","v00Saicode":{"saiCode":"SO02"}}

这些是我的合同"域对象的值.

These are values of my "contract" domain object.

对于UI部分的搜索,您可以为两个日期选择一个操作数(大于或小于或等于)

For the search at the UI part, you can choose for the two dates an operand (greater or less or equals)

所有参数(不带操作数)都是通过GET和Ajax调用发送的.

All the parameters (without the operands) are sent per GET and Ajax call.

Spring MVC Controller看起来像这样:

The Spring MVC Controller looks like this:

    @RequestMapping(value = "/", method = RequestMethod.GET, headers = "Accept=application/json")
    public ResponseEntity<String> getContractsFromSearch(@RequestParam String allRequestParams, ModelMap model){
    //some stuff
    }

我正在寻找技术投入,如何以一种干净而又好的方式将操作数添加到JSON字符串和控制器中.我不想破解一些错误的代码. 如果您需要更多输入,请发表评论. 谢谢!

I'm looking for technical input, how to add the operands to the JSON String and Controller in a clean and good way. I dont want to hack some bad code. If you need more input, please leave a comment. Thanks!

更新1.

获取请求:

allRequestParams={"v00Datvon":"09-07-2014","v00Datbis":"09-07-2014","v00Saicode":{"saiCode":"SO02"}}

更新2.

    {
  "dateFrom": {
    "dateFrom": "someDate",
    "operand": "less"
  },
  "dateTo": {
    "dateTo": "someDate",
    "operand": "greater"
  },
  "season": {
    "season": "someSeason",
    "operand": "equals"
  }
}

更新3 反序列化:

public static Contract fromJsonToContract(String json) {
    return new JSONDeserializer<Contract>().use(Calendar.class, new CalendarTransformer("dd-MM-yyyy HH:mm")).use(null, Contract.class).deserialize(json);
}

推荐答案

我认为您的JSON太冗长:您有一个名为"dateFrom"的条目,而该条目有一个名为"dateFrom"的条目,请尝试使其简单而又扁平".那这个选项呢?:

I think your JSON is too verbose: you have an entry named "dateFrom" that has an entry named "dateFrom", try to keep it simple and "flat". What about this option?:

 {
  "dateFrom": "someDate",
  "operandFrom": "less",

  "dateTo": "someDate",
  "operandTo": "greater",

  "season": "someSeason",
  "operandSeason": "equals"

}

我知道按语义分组数据是很诱人的,但是在GET请求中,您必须考虑处理参数的最佳方法.可以轻松地将数据转换为:

I know that group data by semantic is tempting, but in a GET request you have to think the best way to process the parameters.The data can be easily transformed to:

dateFrom=someDate&operandFrom=less&dateTo=someDate&operandTo=greater&season=someSeason&operandSeason=equals

或仅发送一个以JSON作为值的参数:

or just send one parameter with the JSON as value:

q={"dateFrom": "someDate","operandFrom":"less","dateTo":"someDate","operandTo":"greater","season":"someSeason","operandSeason":"equals"}

但是随后您需要在控制器中对其进行解析.

But then you'd need to parse it in your controller.

这篇关于为使用操作数JSON的搜索构建REST api的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆