Using @Consume with GET request in Jersey Rest

后端 未结 5 656
夕颜
夕颜 2021-02-10 16:05

I\'m trying to bind values in a GET request to a POJO.

The values are parameters in a HTTP GET request. I\'m using JSONP to pass the parameters however it looks like JS

5条回答
  •  無奈伤痛
    2021-02-10 16:44

    I am proposing a more expanded example.

    jQuery client side:

    var argPojo = {
                callback:"myPojoCallback",
                value1:"val1",
                value2:"val2",
                value3:["val1", "val2", "val3"]
            };
    
    var url = 'xxx.xx.xx.xx/testPojo';
        $.ajax({
            type: 'GET',
            async: false,
            jsonpCallback: argPojo.callback,
            url: url,
            data:argPojo,
            contentType: "application/json",
            dataType: 'jsonp',
            beforeSend:function(){
    
                console.log("sending:",argPojo);
            },
            success: function(response) {
                console.log("reciving",response);
    
            },
            error: function(e) {
                console.error("error",e);
            }
        });
    

    on the server

    @Path("testPojo")
    @GET
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/x-javascript")
    public JSONWithPadding testPojo(@InjectParam MyPojo argPojo){
    
        System.out.println(argPojo);
        System.out.println(argPojo.callback);
        System.out.println(argPojo.value1);
        System.out.println(argPojo.value2);
        System.out.println(argPojo.value3);
    
        return new JSONWithPadding(argPojo, argPojo.callback);
    }
    
    the actual class object
    
        public class MyPojo {
    
            @QueryParam("callback")
            public String callback;
    
    
            @QueryParam("value1")
            public String value1;
    
            @QueryParam("value2")
            public String value2;
    
            @QueryParam("value3[]")
            public List value3;
    
            public MyPojo(){}
    
        }
    

    chrome console result

    sending: Object
         callback: "myPojoCallback"
         value1: "val1"
         value2: "val2"
         value3: Array[3]
         __proto__: Object
    
    receiving Object
         callback: "myPojoCallback"
         value1: "val1"
         value2: "val2"
         value3: Array[3]
         __proto__: Object
    

提交回复
热议问题