Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
REST-定义Restful Java服务中JSON数组的名称_Java_Json_Rest_Glassfish - Fatal编程技术网

REST-定义Restful Java服务中JSON数组的名称

REST-定义Restful Java服务中JSON数组的名称,java,json,rest,glassfish,Java,Json,Rest,Glassfish,我在Glassfish服务器上运行的REST Java服务中获得了以下功能: serviceTest.java @Path("/servicetest") public class serviceTest{ @GET @Path("/findall") @Produces(MediaType.APPLICATION_JSON) public List<Person> findAll(){ List <Person> resu

我在Glassfish服务器上运行的REST Java服务中获得了以下功能:

serviceTest.java

@Path("/servicetest")
public class serviceTest{
    @GET
    @Path("/findall")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Person> findAll(){
        List <Person> result = new ArrayList<>();
        result.add(new Person("1", "Charlie");
        result.add(new Person("2", "Mary");
        return result;
        }
    }
public class person {

    private String id;
    private String name;

    public person(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
当我从客户端或web浏览器调用
findAll()
函数时,我得到以下格式的JSON对象:

[
    {
        "id": "1",
        "name": "Charlie"    
    },

    {
        "id": "2",
        "name": "Mary"  
    }
]
但是我需要用一个名称来标识JSON数组,类似这样:

{"person":
    [
        {
            "id": "1",
            "name": "Charlie"    
        },

        {
            "id": "2",
            "name": "Mary"  
        }
    ]
}

我该怎么做。。。?提前感谢…

您可以将人员列表包装在地图中:

public Map<String,List<Person>> findAll(){
    List <Person> list = new ArrayList<>();
    list.add(new Person("1", "Charlie");
    list.add(new Person("2", "Mary");
    LinkedHashMap<String,List<Person>> map = new LinkedHashMap<>();
    map.put("person", list);
    return map;
}
publicmap findAll(){
列表=新的ArrayList();
添加(新的人(“1”、“查理”);
添加(新的人(“2”、“玛丽”);
LinkedHashMap=新建LinkedHashMap();
地图。放置(“人”,列表);
返回图;
}

您可以将人员列表包装在地图中:

public Map<String,List<Person>> findAll(){
    List <Person> list = new ArrayList<>();
    list.add(new Person("1", "Charlie");
    list.add(new Person("2", "Mary");
    LinkedHashMap<String,List<Person>> map = new LinkedHashMap<>();
    map.put("person", list);
    return map;
}
publicmap findAll(){
列表=新的ArrayList();
添加(新的人(“1”、“查理”);
添加(新的人(“2”、“玛丽”);
LinkedHashMap=新建LinkedHashMap();
地图。放置(“人”,列表);
返回图;
}
使用单个字段将
列表
包装在一个类中:

public class PersonResponse {

    private List<Person> person = new ArrayList<Person>();

    public PersonResponse(List<Person> person) {
        this.person = person;
    }

}
公共类PersonResponse{
private List person=new ArrayList();
公众人物回应(名单人物){
这个人=人;
}
}
并将您的休息方法更改为:

public PersonResponse findAll(){
    List <Person> result = new ArrayList<>();
    result.add(new Person("1", "Charlie");
    result.add(new Person("2", "Mary");
    return new PersonResponse(result);
    }
}
publicpersonresponse findAll(){
列表结果=新建ArrayList();
结果。添加(新人员(“1”、“查理”);
结果。添加(新的人(“2”、“玛丽”);
返回新的PersonResponse(结果);
}
}
使用单个字段将
列表
包装在一个类中:

public class PersonResponse {

    private List<Person> person = new ArrayList<Person>();

    public PersonResponse(List<Person> person) {
        this.person = person;
    }

}
公共类PersonResponse{
private List person=new ArrayList();
公众人物回应(名单人物){
这个人=人;
}
}
并将您的休息方法更改为:

public PersonResponse findAll(){
    List <Person> result = new ArrayList<>();
    result.add(new Person("1", "Charlie");
    result.add(new Person("2", "Mary");
    return new PersonResponse(result);
    }
}
publicpersonresponse findAll(){
列表结果=新建ArrayList();
结果。添加(新人员(“1”、“查理”);
结果。添加(新的人(“2”、“玛丽”);
返回新的PersonResponse(结果);
}
}

很抱歉,但现在我收到了此错误:
HTTP状态500-内部服务器错误。服务器遇到了一个内部错误,使其无法完成此请求。
@CyborgNinja23在日志文件中搜索堆栈跟踪,但现在我收到了此错误:
HTTP状态500-内部服务器错误。服务器遇到一个内部错误阻止它完成此请求。
@CyborgNinja23搜索日志文件以查找堆栈跟踪出于某种原因,当我将其包装在此类中时,它会给出“状态500-内部服务器错误”出于某种原因,当我将其包装在此类中时,它会给出“状态500-内部服务器错误”