开发者

Jersey RESTfull service with XML (encapsulation outside the JSON-Object needed)

开发者 https://www.devze.com 2023-04-04 14:02 出处:网络
I try to implement a restful webservice in Java together with Jersey. To communicate between the client- and server-side i´m watching out for XML.

I try to implement a restful webservice in Java together with Jersey. To communicate between the client- and server-side i´m watching out for XML. I already tried JSON.

When using JSON, the encapsulation is in the POJO-Object like:

@XmlRootElement
public class MyPojo {
    public int a;
    public int[] b;
}

Then I just got a header in the Rest-Class like

public String classname(MyPojo p)

But I need a header like

public String classname(int a, int [] b)

to create Form-Elements automa开发者_C百科tically by reading the Rest-Headers. An example showed me that:

@Consumes("application/xml")
public classname methodname(@QueryParam("a") Integer a, @QueryParam("b") IntArray b)

should work. Question: How can I create a XML-Request (like JSON.stringify() in XML) for this method? Is there maybe a better way doing this?


Not sure if I understand the question, but will try to provide some hints - hopefully at least some of it will be relevant. If not, please share more about your app (i.e. is this for GET or POST requests? why is it important to have 2 separate parameters, etc.)

If you need to send XML or JSON in the request entity (e.g. in a POST request), then it is not possible to retrieve these in multiple parameters - you have to live with the single object parameter as you have above. What you could do is the following:

@POST
@Consumes("application/xml")
public ClassName postMethod(MyPojo p) {
    return postMethod(p.a, p.b);
}

public ClassName postMethod(int a, int[] b) {
    // do something
}

Or, if you don't really need XML/JSON, if you are POSTing using HTML forms, typically you do the following:

@POST
@Consumes("application/x-www-form-urlencoded")
public ClassName postMethod(@FormParam("a") Integer a, @FormParam("b") String b /*I think arrays are not supported - will have to parse it yourself*/) {
    // do something
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号