📄 index.xtp
字号:
public class FlickrResponse { @XmlAttribute public String stat = "ok"; @XmlAnyElement(lax=true) public FlickrPayload payload; public String toString() { return "FlickrResponse[stat=" + stat + ", payload=" + payload + "]"; }}</example><p>Notice the @XmlRootElement annotation on the class. This annotation sets thethe element name to <code>rsp</code>. Next, there is a status field whichis an attribute in the element. JAXB simply uses the field name in this caseto set the attribute when the @XmlAttribute annotation is present. Finally,the payload is annotated with @XmlAnyElement. This annotation isnecessary since <code>FlickrPayload</code> is an interface. @XmlAnyElementallows the marshalling of an interface or abstract class. The <code>lax</code>property allows JAXB to use known classes to unmarshal an interface orclass. How to let JAXB know about such class is covered at the nextsection.</p><p>FlickrPayload is an empty interface, used only for typing in this example.FlickrUser implements FlickrPayload and is the response payload used by the <code>flickr.people.findByEmail</code> and<code>flickr.people.findByUsername</code> methods. The XML response fromthe section "REST Bindings" above corresponds to this class.</p><example title="FlickrUser.java">package example.data;import javax.xml.bind.annotation.*;@XmlRootElement(name="user")public class FlickrUser implements FlickrPayload { @XmlAttribute public String nsid; @XmlElement public String username; public String toString() { return "FlickrUser[nsid=" + nsid + ", username=" + username + "]"; }}</example><p>The <code>FlickrUser</code> class is somewhat simpler thanthe <code>FlickrResponse</code> example above in that it only has a rootelement, <code><user></code>, a singleattribute, <code>nsid</code>, and a single child element, <code><username></code>. The attribute and child element here are bothsimply strings. <code>FlickrPhotos</code> is a more complex class witha variable number of child elements.</p><example title="FlickrPhotos.java">package example.data;import java.util.*;import javax.xml.bind.annotation.*;@XmlRootElement(name="photos")public class FlickrPhotos implements FlickrPayload { @XmlAttribute public int page; @XmlAttribute public int pages; @XmlAttribute public int perpage; @XmlAttribute public int total; @XmlElement(name="photo") public List<Photo> photos = new ArrayList<Photo>(); public static class Photo { @XmlAttribute public String id; @XmlAttribute public String owner; @XmlAttribute public String secret; @XmlAttribute public int server; @XmlAttribute public String title; @XmlAttribute public int ispublic; @XmlAttribute public int isfriend; @XmlAttribute public int isfamily; public String toString() { return "Photo[id=" + id + ", " + "owner=" + owner + ", " + "secret=" + secret + ", " + "server=" + server + ", " + "title=" + title + ", " + "ispublic=" + ispublic + ", " + "isfriend =" + isfriend + ", " + "isfamily=" + isfamily + "]"; } } public String toString() { StringBuilder sb = new StringBuilder(); sb.append("FlickrPhotos[page=" + page + ", "); sb.append( "pages=" + pages + ", "); sb.append( "perpage=" + perpage + ", "); sb.append( "total=" + total + ", "); sb.append( "photos=("); for (Photo photo : photos) { sb.append(photo.toString()); sb.append(' '); } sb.append(")]"); return sb.toString(); }}</example><p>Here there are four attributes of the root element and a list of childelements representing the photos. Each photo has a set of attributesas well. By default, JAXB serializes lists as unadorned sequences ofelements. For example, a FlickrPhotos object with two photos might havethe following XML:</p><example title=""><rsp stat="ok"> <photos total="2" perpage="10" pages="1" page="1"> <photo isfamily="0" isfriend="0" ispublic="1" title="Our wedding" server="2" secret="x123456" owner="12345678901@N01" id="3041"/> <photo isfamily="0" isfriend="1" ispublic="0" title="Best friends" server="1" secret="y123456" owner="12345678901@N01" id="3042"/> </photos></rsp></example><p>The remaining payload classes, <code>FlickrError</code>, <code>FlickrGroups</code>, and <code>FlickrPerson</code>, are annotatedsimilarly.</p></s1><s1 title="Client configuration"><p>A REST client is configured using the <web-service-client> tag, just likewith SOAP and Hessian clients. The only difference is that the URL mustuse the "rest:" prefix. The Resin SOA connects to the service using thegiven URL and places a proxy instance into JNDI. Any clients may accessthe service then by doing a JNDI lookup, then calling methods on the returnedobject.</p><example title="resin-web.xml"><web-service-client jndi-name="rest/flickr"> <url>rest:${webApp.url}/flickr/rest/</url> <interface>example.FlickrAPI</interface> <jaxb-package>example.data</jaxb-package></web-service-client></example><p>Notice the <jaxb-package> tag. In order for JAXB to marshal andunmarshal objects, it must know all the classes it might encounter.There are two ways to inform JAXB about the classes: either by a list ofpackage names or an explicit list of classes. In this example, all of theclasses that JAXB will use are in the package <code>example.data</code>,so the <jaxb-package> tag is the easiest way. The <jaxb-package>tag may be used multiple times to add classes from several packages.(It is also possible to use multiple <jaxb-class> tags to list theclasses explicitly.)</p><p>Simply listing a <jaxb-package> may not be appropriate in all cases. In fact in this example, notice that FlickrPayload is simply an emptyinterface and is not JAXB-annotated. Thus JAXB should load only thoseclasses in the <code>example.data</code> package that are annotated. The JAXB standard specifies that a file named "jaxb.index" will be read by JAXB whenever a package is given. This file should be placed in thepackage directory. The list of classes should be relative to the package,without a .java or .class suffix, and should be separated by newlines.For this example, the jaxb.index is the following:</p><example title="jaxb.index">FlickrErrorFlickrGroupsFlickrPersonFlickrPhotosFlickrResponseFlickrUser</example></s1><s1 title="Testing the interface"><p>An example implementation of the service is provided here for testingpurposes only. It returns sample data upon receiving REST requests and does not actually store or display images. Because setting up a RESTservice is covered in another tutorial, only the client side of the RESTconnection is explained fully here.</p><p>The demonstration JSP simply looks up the proxy interface in JNDI, thencalls the methods with sample queries. The calls and their results areprinted.</p><p><i>flickr is a trademark of Yahoo! Inc.</i></p></s1> </body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -