How to send and receive JSON data from a restful webservice using Jersey API

后端 未结 3 1925
孤城傲影
孤城傲影 2020-12-06 00:53
@Path(\"/hello\")
public class Hello {

    @POST
    @Path(\"{id}\")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public          


        
3条回答
  •  粉色の甜心
    2020-12-06 01:25

    For me, parameter (JSONObject inputJsonObj) was not working. I am using jersey 2.* Hence I feel this is the

    java(Jax-rs) and Angular way

    I hope it's helpful to someone using JAVA Rest and AngularJS like me.

    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.APPLICATION_JSON)
    public Map methodName(String data) throws Exception {
        JSONObject recoData = new JSONObject(data);
        //Do whatever with json object
    }
    

    Client side I used AngularJS

    factory.update = function () {
    data = {user:'Shreedhar Bhat',address:[{houseNo:105},{city:'Bengaluru'}]};
            data= JSON.stringify(data);//Convert object to string
            var d = $q.defer();
            $http({
                method: 'POST',
                url: 'REST/webApp/update',
                headers: {'Content-Type': 'text/plain'},
                data:data
            })
            .success(function (response) {
                d.resolve(response);
            })
            .error(function (response) {
                d.reject(response);
            });
    
            return d.promise;
        };
    

提交回复
热议问题