Red Hat Training

A Red Hat training course is available for Red Hat JBoss Enterprise Application Platform

15.14.2. Atom プロバイダーでの JAXB の使用

org.jboss.resteasy.plugins.providers.atom.Content クラスを使用すると、コンテンツのボディーであるアノテーション付きのオブジェクトのマーシャリングおよびマーシャリング解除できます。エントリーのコンテンツの本文として Customer オブジェクトが添付されたエントリーを送信する例を参照できます。
 
@XmlRootElement(namespace = "http://jboss.org/Customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer
{
   @XmlElement
   private String name;

   public Customer()
   {
   }

   public Customer(String name)
   {
      this.name = name;
   }

   public String getName()
   {
      return name;
   }
}

@Path("atom")
public static class AtomServer
{
   @GET
   @Path("entry")
   @Produces("application/atom+xml")
   public Entry getEntry()
   {
      Entry entry = new Entry();
      entry.setTitle("Hello World");
      Content content = new Content();
      content.setJAXBObject(new Customer("bill"));
      entry.setContent(content);
      return entry;
   }
}
Content.setJAXBObject () メソッドは、適切にマーシャリングするために JavaJAXB オブジェクトに送り返すコンテンツオブジェクトを指定するために使用されます。XML とは異なるベース形式 (例: application/atom+json) を使用している場合、添付の Mission オブジェクトは同じ形式でマーシャリングされます。atom ドキュメントを入力として用意している場合は、Content.getJAXBObject(Class clazz) メソッドを使用して Content から正確な JAXB オブジェクトを抽出することもできます。これは、入力アトムドキュメントとコンテンツからの Customer オブジェクトの抽出の例です。
 
@Path("atom")
public static class AtomServer
{
   @PUT
   @Path("entry")
   @Produces("application/atom+xml")
   public void putCustomer(Entry entry)
   {
      Content content = entry.getContent();
      Customer cust = content.getJAXBObject(Customer.class);
   }
}