users@jersey.java.net

Re: [Jersey] How to add XML node to existing JAXB annotatied class?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 13 Feb 2009 10:53:54 +0100

On Feb 13, 2009, at 6:38 AM, bndi wrote:

> Hi all.
>
> Product.java :
> ---------------------------------------
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlRootElement
> public class Product {
> private int code;
> private String name;
> }
> ---------------------------------------
>
> ProductResource.java :
> ---------------------------------------
> public class ProductResource {
> @Path("info/")
> public Response getProductInfo() {
> Product p = new Product();
> p.setCode(1234);
> p.setName("test");
> // how to add XML node this point? e.g. "total=1000"
> return ResponseImpl.ok(p).build();
> }
> }
> ---------------------------------------
>
> expected result :
> ---------------------------------------
> <product>
> <code>1234</code>
> <name>test</name>
> <total>1000</total>
> </product>
> ---------------------------------------
>
> how to do this?
>
You need to modify your Product class to have a field called "total".

If you cannot modify that class but can extend from it then i think
you can do:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class ExtendedProduct extends Product {
   private int total;
}



> ==========================================
> https://jersey.dev.java.net/servlets/ReadMsg?list=users&msgNo=3785
>
> Paul. wrote.
> > But. given that the client states what the function should be in the
> > query parameter why can't it do the work itself and wrap the JSON
> > around the function? why it is necessary to do this on the server
> side?
>
> I'm provide json format data to OpenAPI service.
> but what if problem is cross domain security error. e.g) my domain
> is 'aaa.com' and user's domain is 'bbb.com'
> therefore wrap javascript callback function name to json message
> body. e.g) zzz({"code":"12312","name":"yyy"})
> and using below javascript to avoid cross domain problem.
> ------------------------------------------
> aaa : function()
> {
> obj.s = document.createElement('script');
> obj.s.type ='text/javascript';
> obj.s.charset ='utf-8';
> obj.s.src = 'http://aaa.com?callback=obj.bbb' +
> encodeURI(obj.q.value);
> }
> },
>
> bbb : function(z)
> {
> // TODO
> },
> ------------------------------------------
>

I see. Jakub has added JSONP support in 1.0.2:

https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/api/jersey/com/sun/jersey/api/json/JSONWithPadding.html
http://download.java.net/maven/2/com/sun/jersey/samples/jsonp/1.0.2/jsonp-1.0.2-project.zip

Paul.