从Rest API中的HashMap <string,object =“”>返回Json?

时间:2018-03-02 18:46:17

标签: java json rest hashmap

我是java的新手,我试图制作一个get Rest API。我无法将结果转换为Json的问题。这是代码的一部分:

&#13;
&#13;
@Path("/")
public class Search{
	@GET
    @Path("/result")
    @Produces({MediaType.APPLICATION_JSON })
    public Response getAllResults(){
				
        Map<String, DashboardSpaceResult> map = new HashMap<String, DashboardSpaceResult>();
        Map<String, List<Page>> objectsBySpace = SPGSearchUtils.searchAllPagesForLabels(MetaData.GLOSSARY_LETTER, null);   	
	    
	    for (String spaceKey : objectsBySpace.keySet()) {
	    	
	    	DashboardSpaceResult glossarySpaceResult = new DashboardSpaceResult();
            Page parentPage = objectsBySpace.get(spaceKey).get(0).getParent();
	    	
            glossarySpaceResult.setHome(parentPage);
            glossarySpaceResult.setSpace(objectsBySpace.get(spaceKey).get(0).getSpace());
            
            map.put(spaceKey, glossarySpaceResult);
		}
	    JSONObject jsonObject = new JSONObject(map);
	    return Response.ok(jsonObject).build();
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

尝试

return Response.ok(map).build();

它应该自动使用provider转换为json