Jersey/JAXB/XML/JSON: Adding elements to documents created from POJOs
I'm new to Jersey, JAXB, and JSON, and fairly green with XML. I'm playing with a simple Web service (which strives to be RESTful).
Given the following POJO:
@XmlRootElement
public class POJO {
.
.
@XmlElement
public String getProp1 {
return prop1;
}
@XmlElement
public String getProp2 {
return prop2;
}
}
and the following resource class:
@Path("/resource)
public class Resource {
@Path("/pojo")
@GET
@Produces({"application/xml", "application/json"})
public POJO getPojo() {
POJO pojo = new POJO();
pojo.setProp1("foo");
pojo.setProp2("bar");
return pojo;
}
}
HTTP requests to the proper URI return the XML and JSON I expect:
<?xml version="1.0" encoding="UTF-8"?>
<pojo>
<prop1>foo</prop1>
<prop2>bar</prop2>
</pojo>
{"prop1":"foo","prop2":"bar"}
Next, I want to update the service to produce the following output:
开发者_Go百科<?xml version="1.0" encoding="UTF-8"?>
<pojo>
<prop1>foo</prop1>
<prop2>bar</prop2>
<link>http://coolURI</link>
</pojo>
{"prop1":"foo","prop2":"bar","link":"http://coolURI"}
where "coolURI" is a hypermedia pointer to the next resource, and obviously not a property of class POJO, so its value will be marshaled to XML differently. Please point me toward the mechanism(s) I need to employ and I should be good to go. Thanks for your help.
You must return something else than a POJO from your getPojo() method. Either an extended POJO that includes the link, or a Response object that somehow inserts the link.
精彩评论