2011-06-16 54 views
4

我只是简单地想从我的服务器返回一个JSON对象(使用ajax)到客户端 - 所以我'米能够在客户端读取数据如何从我的休息服务发送json对象,以便我可以解析出客户端javascript

@GET 
    @Produces("application/json") 
    @Consumes("application/json") 
    @Path("/getStatus/") 

    public void getStatus(
     @Context HttpServletRequest request, 
     @Context HttpServletResponse response) throws ServletException, 
     IOException 
    { 

     //create the JSON Object to pass to the client 
     JSONObject object=new JSONObject(); 

     response.setContentType("text/javascript");  

     try 
     { 
      object.put("name", nameDataFromClass); 
      object.put("status",someData); 

     } 
     catch(Exception e) 
     { 
      throw new ServletException("JSON Hosed up"); 
     } 

     String json = object.toString(); 
     response.getOutputStream().println(json); 
    } 

这将是在JSP客户端我想提取出来的数据

<html> 
<head> 

<!-- Calls in jQuery file --> 
<script src="jquery.js"></script> 

<title>JQuery Test</title> 

<script> 

    $.getJSON("http://localhost:8080/scout/rest/admin/mamba/getStatus", 
    function(json) 
    { 
     alert("Server naame: " + json.name); 
    }); 



</script> 

</head> 
<body> 



</body> 
</html> 
+0

等什么不会:

public class Mystatus{ public String name; public String status; public Mystatus(){} // a default empty constructor is needed public Mystatus(String name,String status){ this.name=name; this.status=status; } } 

然后从你的RESTful Web服务返回这个对象为你工作? – mkro 2011-06-16 20:25:24

+0

客户端没有收到数据。我的宁静的java应该有回报吗?是否有一些我缺少的jquerry/ajax作品? – stackoverflow 2011-06-16 20:27:00

+0

请求实际到达java层吗? – mkro 2011-06-16 20:28:00

回答

0
@GET 
    @Produces("application/json") 
    @Consumes("application/json") 
    @Path("/status") 
    // server:8080/server/rest/status 
    public String getStatus(
     @Context HttpServletRequest request, 
     @Context HttpServletResponse response) throws Exception 
    { 

    // Create a string to hold JSON 
    String json; 

     Collection<Server> svr = SomeHashMap.getStuff().values(); 

     JSONArray jArray = new JSONArray(); 

     for (Server i : svr) 
     { 
     JSONObject m = new JSONObject(); 

     ServerStatus status = i.getStatus(); 

     m.put("id", i.getId()); 
     m.put("name", i.getName()); 
     m.put("status", status.getState()); 

     jArray.add(m); 
     } 

     json = jArray.toString(); 
    } 

    response.setContentType("text/javascript"); 
    response.getOutputStream().print(json); 
    response.flushBuffer(); 

    return null; 
} 

的index.jsp

<head> 
<script src="jquery-1.6.js"></script> 
    <!--AJAX FOR STATUS PAGE REFRESH --> 
    <script type="text/javascript"> 

    //when page is ready do the following 
    $(document).ready(function() 
    { 
     // Disable caching of AJAX responses 
     $.ajaxSetup ({cache: false}); 

     //set interval of refresh 
     setInterval(doAjaxStuff, 1000); 

     //function to call to fire off ajax 
     function doAjaxStuff() 
     { 
      $.ajax 
      ({ 
       url: "status", // <-- this refers to the Controller function above called "status()" 
       dataType: 'json', 
       success: function(json) 
       { 
        //traverse throught each element in the incoming JSON object 
        for(var i = 0; i< json.length; i++) 
        { 
         if(json[i].status == "ACTIVE") 
         { 
          $("#Status"+json[i].id).html("Running"); 
         } 


        }    
       } 
      }); 
     } 
    }); 
    </script> 

</head> 
+0

所有荣誉mkro以上在评论 – stackoverflow 2011-08-15 18:42:28

1

杰克逊库应注意在页面上将json对象编组到对象中,反之亦然。只需创建一个简单的POJO,就像这样:

@GET 
@Produces("application/json") 
@Consumes("application/json") 
@Path("/getStatus/") 

public Mystatus getStatus(
    @Context HttpServletRequest request, 
    @Context HttpServletResponse response) 
{ 
    response.setContentType("text/javascript"); 
    return new Mystatus("Hello","World"); 
} 
相关问题