如何从javascript发送Json post请求到apache CXF Rest Service

时间:2012-02-10 17:38:06

标签: javascript json web-services cxf webservice-client

是否有人可以共享javascript示例以将json post请求发送到Apache CXF Rest Service。 我想将这个java脚本用于phonegap API。

2 个答案:

答案 0 :(得分:2)

这是我用来调用其余服务帖子方法的帖子

$.ajax({
  url: "http://localhost:8080/restService",
  type: "POST",
  data: { data1: "xyz", data2: "xyz"},
  success: function(data){
            alert(data);
              }
});

这是服务代码段:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/restService")
public Collection<Obj> getPost(
        @FormParam("data1") String data1,
        @FormParam("data2") String data2){ //implementation }

答案 1 :(得分:-1)

Create json object

var dataObj ={"data1": "xyz", "data2" : "xyz"};

$.ajax({
  url: "http://localhost:8080/restService",
  type: "POST",
  data: dataObj,
  success: function(data){
            alert(data);
              }
});



@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/restService")
public Collection<Obj> getPost(
        @RequestBody DataObject dataObj){ //implementation }


Your DataObject class will look like

Class DataObject {
     private String data1;
     private String data2;

     public void setData1(String data1){
        this.data1 = data1;
     }

     public String getData1(){
         return this.data1;
     }

public void setData2(String data2){
        this.data2 = data2;
     }

     public String getData2(){
         return this.data2;
     }

}