将JSON对象传递给Web服务方法

时间:2015-02-25 05:25:43

标签: json web-services

是否有人知道如何在webservice方法中传递json对象。以下是Iam目前实施的方式..

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/addSamples/{name1}/{age1}/{location1}/{name2}/{age2}/{location2}")
public String addSample(@PathParam("name1") string name1,
        @PathParam("age1") int age1,
        @PathParam("location1") string location1,
        @PathParam("name2") string name2,
        @PathParam("age2") int age2,
        @PathParam("location2") string location2) {

    SampleModel sfm = new SampleModel();
    sfm.setName1(name1);
    sfm.setAge1(age1);
    sfm.setLocation1(location1);

    SampleModel sfm2 = new SampleModel();
    sfm2.setName1(name2);
    sfm2.setAge1(age2);
    sfm2.setLocation1(location2);

    SampleModel model = sampleService.addSample(sfm);
    SampleModel model1 = sampleService.addSample(sfm2);

    return "true";
}

但我想将name1,age1,location1,name2,age2和location 2作为单个json对象传递给这样的东西..

[
    {
        "name1": "abc",
        "age1": "23",
        "location1": "XYZ"
    },
    {
        "name2": "def",
        "age2": "33",
        "location2": "MNO"
    }
]  

有没有人试过这个..如果是的话请建议我..
提前谢谢你 问候
VHC

1 个答案:

答案 0 :(得分:0)

创建此类对象的数组,然后将此数组传递给service。