日期:2014-05-17  浏览次数:20631 次

jersey获取json参数
谁给个demo啊?
我这样写获取不到客户端的json数据

前台请求:
JScript code

$.ajax( {
        url : '/Test/services/update',
        type : 'post',
        data : '{"1":"1","2":"2"}',
        dataType : 'json',
        contentType:'application/json',
        async : false,
        success : function(data) {
            alert("success");
            alert(data.name);
            alert(data.age);
        },
        error : function() {
            alert("ajax error");
        }
    });



后台的jsrsey处理,如何获取前台的json数据:data : '{"1":"1","2":"2"}',

Java code

@POST
    @Path("/update")
    @Produces("application/json")
    
    //@Produces(MediaType.APPLICATION_JSON)
    public Person update(HashMap map) {

        System.out.println("aaaaaaaaaaa");
        System.out.println(map.get("1"));
        System.out.println(map.get("2"));//这里取不到,而且还报错
        
        
//        Map<String,Person> aa = new HashMap<String,Person>();
//        aa.put("1", new Person("aaa","123"));
//        aa.put("2", new Person("BBB","456"));
        System.out.println("bbbbbbbbbbb");
        //return  new GenericEntity<Map<String, Integer>> (aa){};

        return new Person("test","test");
    }




报错:SEVERE: A message body reader for Java type, class java.util.HashMap, and MIME media type, application/json;charset=UTF-8, was not found

415 Unsupported Media Type
61ms

------解决方案--------------------
Java code

@GET
    @Path("/requestTest")
    @Produces("application/json")
    public Person update(@Context HttpServletRequest request) {

        System.out.println("request:" + request.getParameter("a11"));
        System.out.println("request:" + request.getParameter("a22"));
        System.out.println("request:" + request.getQueryString());

        return new Person("testPerson", "33");
    }