【问题标题】:issues about json format returned by spring restful?关于spring restful返回的json格式的问题?
【发布时间】:2016-11-08 08:10:59
【问题描述】:
Class EntityDataSet {
      public List<EntityRow> getData() {
        return rowList;     
}

class EntityRow {
      private Map<String, Object> rowData;

      public Map<String, Object> getRowData() {
        return rowData;
    }
}

我在restful控制器中写了一个方法,如下所示。

@RequestMapping(value = "/entityData", method = RequestMethod.POST, headers = "Accept=application/json")
public EntityDataSet getEntityData(@RequestBody QueryObject query) throws Exception{
    return entityManager.generateEntityDataByName(query);
}

返回的 json 如下所示。

{
    "data": [
        {
            "rowData": {
                "FISCAL_YEAR_END": null,
                "HOME_COUNTRY": null
            }
        },
        {
            "rowData": {
                "FISCAL_YEAR_END": null,
                "HOME_COUNTRY": "10"
            }
        },
        ..................

但我想得到的是如下所示。

{
    "data": [
        {
                "FISCAL_YEAR_END": null,
                "HOME_COUNTRY": null
        },
        {
                "FISCAL_YEAR_END": null,
                "HOME_COUNTRY": "10"
        },

我该如何实现它?谢谢。

【问题讨论】:

  • 它只是给出想法,不要立即返回,取对象并使用json api转换然后返回。
  • 将您的 EntityRow 类更改为具有两个属性,而不是返回 rowData 对象
  • 但是hashmap中的key会改变,不是固定的

标签: json spring rest spring-mvc spring-rest


【解决方案1】:

可以使用 EntityRow 类中的 @JsonAnySetter@JsonAnyGetter 注释来完成(如果使用 Jackson):

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

public class EntityDataSet {

    @JsonProperty("data")
    private List<EntityRow> rowList;

    /**
     * @return the rowList
     */
    public List<EntityRow> getRowList() {
        return rowList;
    }

    /**
     * @param rowList the rowList to set
     */
    public void setRowList(List<EntityRow> rowList) {
        this.rowList = rowList;
    }

    /**
     * 
     * @param row
     */
    public void add(EntityRow row){
        if(this.rowList == null){
            this.rowList = new ArrayList<EntityRow>();
        }
        this.rowList.add(row);
    }
}

这是关键类,带有@JsonAnySetter 和@JsonAnyGetter 注释的EntityRow

import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;


public class EntityRow {

    private Map<String, Object> rowData;

    /**
     * @param rowData the rowData to set
     */
    @JsonAnySetter
    public void setRowData(Map<String, Object> rowData) {
        this.rowData = rowData;
    }

    @JsonAnyGetter
    public Map<String, Object> getRowData() {
        return rowData;
    }
}

一个主要的测试它:

import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test4 {

    public static String getJsonString(Object o){
        ObjectMapper mapper = new ObjectMapper();

        //For testing
        try {
            //Convert object to JSON string
            String jsonInString = mapper.writeValueAsString(o);
            //System.out.println(jsonInString);
            return jsonInString;


        } catch (JsonProcessingException e){
            e.printStackTrace();
        } 
        return null;
    }

    public static void main(String args[]){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("FISCAL_YEAR_END", null);
        map.put("HOME_COUNTRY", null);

        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("FISCAL_YEAR_END", null);
        map2.put("HOME_COUNTRY", "10");

        EntityRow row1 = new EntityRow();
        row1.setRowData(map);

        EntityRow row2 = new EntityRow();
        row2.setRowData(map2);

        EntityDataSet entityDataSet = new EntityDataSet();
        entityDataSet.add(row1);
        entityDataSet.add(row2);

        System.out.println(Test4.getJsonString(entityDataSet));
    }
}

最后,这是输出:

    {"data":
           [
             {
                 "FISCAL_YEAR_END":null,
                 "HOME_COUNTRY":null
             },
             {
                 "FISCAL_YEAR_END":null,
                 "HOME_COUNTRY":"10"
             }
           ]
    }

【讨论】:

  • 非常感谢,它有效。但我发现不需要'@JsonProperty("data")'。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 2017-01-28
  • 2013-08-08
  • 1970-01-01
  • 2023-02-13
  • 1970-01-01
相关资源
最近更新 更多