简体   繁体   English

如何使带有JSON数据的post ajax调用到Jersey休息服务?

[英]How to make post ajax call with JSON data to Jersey rest service?

I have been through this link . 我已经通过这个链接 but this did not helped me out. 但这并没有帮助我。

I am using jersey lib v1.17.1. 我正在使用jersey lib v1.17.1。 My jersey rest service: 我的球衣休息服务:

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Path("/post1")
public ResponseBean post1(@QueryParam("param1")String param1)
{
    return ResponseFactory.createResponse(param1, "TEST", "TEST", null, true);
}

url is: /test/post1 网址是: /test/post1

My ajax call: 我的ajax电话:

var d = {"param1":"just a dummy data"};
    $.ajax({
        type : "POST",
        url : "http://localhost:7070/scl/rs/test/post1",
        contentType :"application/json; charSet=UTF-8",
        data : d,
        dataType : "json"
    })
    .done(function(data){
        console.log(data);
    })
    .fail(function(data){
        console.log(data);
    });

It hits to my rest service but as param1 I am alway getting null value. 它影响了我的休息服务,但作为param1我总是得到空值。 The alternate solution is to add JavaBean with @XMLRootElement which will marshal/unmarshal the java object to json and vice versa, but I do not want to use this. 另一种解决方案是添加带有@XMLRootElement JavaBean, @XMLRootElement将Java对象编组/解组为json,反之亦然,但是我不想使用它。
Is there any way to post data and receive it using appropriate annotation like @QueryParam or something like that ? 是否有任何方法可以发布数据并使用@QueryParam类的适当注释接收数据? Please help 请帮忙

Your server-side code should be like this: 您的服务器端代码应如下所示:

@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Path("/post1")
public ResponseBean post1(Data param1)
{
    return ResponseFactory.createResponse(param1, "TEST", "TEST", null, true);
}

where Data is a (POJO) class annotated with @XmlRootElement and corresponds to the JSON data what your client will send (ie, have a param1 field with getter and setter). 其中, Data是(POJO)类,其@XmlRootElement批注,并且对应于客户端将发送的JSON数据(即,具有带有getter和setter的param1字段)。 The JAX-RS implementation will unmarshall the body of the POST into an instance of Data . JAX-RS实现会将POST的正文解组为Data的实例。

@QueryParam annotation is used ot retrieve the query params in a (usually) GET requests. @QueryParam批注用于在(通常) GET请求中检索查询参数。 Query params are the params after the question mark ( ? ). 查询参数是问号( ? )之后的参数。 Eg: @QueryParam("start") String start will map be set to 1 when the following request is processed: GET http://foo.com/bar?start=1 , but this is not what you're doing in your case, AFAICS. 例如: @QueryParam("start") String start处理以下请求时, @QueryParam("start") String start将映射为1GET http://foo.com/bar?start=1 ,但这不是您正在执行的操作案例,AFAICS。

You can simply take Post dat as a string and then you can parse it using JSONObject.
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Path("/post1")
    public Response postStrMsg(String msg) {
        String output = "POST:Jersey say : " + msg;
        return Response.status(200).entity(output).build();
    }

@XMLRootElement是实现此目的的方法,因为必须先对json进行封送处理,然后才能使用其任何元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM