休息服务:无法从@Comsumes()获取全部数据(javax.ws.rs.Comsumes提供)

时间:2018-08-25 20:33:04

标签: java json rest

我有一个Java的rest服务,调用REST服务的请求包含一个JSON对象。

我发现sub_array_1和sub_array_2是空列表,但其他类成员的值都在receviedObj中。

@Consumes()不能解析1个以上级别的json对象吗? 如何在sub_array_1和sub_array_2中获取正确的数据?

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/* ... */

@POST
@Path("")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response sendRequest(ClassB receviedObj) {
    /*  ...  */
}

json对象喜欢:

{
  "item_1": "value_1",
  "item_2": "value_2",
  "item_3": "value_3",
  "otherItems": [
      {
          "subitem_1": "subvalue_1",
          "subObject": {
            "name": "aaabbb",
            "sub_array_1": [
                {
                    "data_1": "d11",
                    "data_2": "d12",
                    "data_3": "d13"
                },
                {
                    "data_1": "d21",
                    "data_2": "d22",
                    "data_3": "d23"
                }
            ],
            "sub_array_2": [
                {
                    "tag_1": "v1",
                    "tag_2": "v2",
                    "tag_3": "v3",
                    "tag_4": "v4"
                }
            ]
        }
    }
  ]
}

java类:

public class ClassB {
    public String item_1;
    public String item_2;
    public String item_3;
    public List<otherItems> otherItems;
}    

public class otherItems{
    public String subitem_1;
    public subObject subObject;

    public otherItems(){
        this.subitem_1 = "";
        this.subObject = new subObject();
    }

    // ================================
    public static class subObject{
        public String name;
        public List<sub_array_1>  sub_array_1;
        public List<sub_array_2>  sub_array_2;

        public subObject(){
            this.name= "";
            this.sub_array_1 = new ArrayList<sub_array_1>();
            this.sub_array_2 = new ArrayList<sub_array_2>();
        }
    }

    // =========================================
        public static class sub_array_1 {
            public String data_1;            
            public String data_2;
            public String data_3;

            public String getdata_1() {
                return data_1;
            }

            public String getdata_2() {
                return data_2;
            }

            public String getdata_3() {
                return data_3;
            }
        }
        public static class sub_array_2 {
            public String tag_1;
            public String tag_2;
            public String tag_3;
            public String tag_4;

            public String gettag_1() {
                return tag_1;
            }

            public String gettag_2() {
                return tag_1;
            }

            public String gettag_3() {
                return tag_1;
            }

            public String gettag_4() {
                return tag_1;
            }

        }
}

2 个答案:

答案 0 :(得分:0)

我认为您发送的JSON不适合ClassA,因为它有一些ClassA中不存在的参数。像item_1,item_2之类的参数。根据我的测试,遵循JSON应该可以ClassA很好地填充。

{
  "subitem_1": "subvalue_1",
  "subObject": {
    "name": "aaabbb",
    "sub_array_1": [
      {
        "data_1": "d11",
        "data_2": "d12",
        "data_3": "d13"
      },
      {
        "data_1": "d21",
        "data_2": "d22",
        "data_3": "d23"
      }
    ],
    "sub_array_2": [
      {
        "tag_1": "v1",
        "tag_2": "v1",
        "tag_3": "v1",
        "tag_4": "v1"
      }
    ]
  }
}

因此,您需要更改ClassA以合并其他字段,或者需要使用上述JSON。另外,我建议您阅读有关Java的编码实践和命名约定的文章

答案 1 :(得分:0)

最后,我仍然无法弄清楚为什么JSX-RS无法将JSON正确转换为Java类。

我使用Gson进行转换并使用正确的数据获取对象。