dev@jersey.java.net

mediatype attribute missing from representation element in generated WADL file

From: Ayub Khan <Ayub.Khan_at_Sun.COM>
Date: Tue, 17 Jul 2007 13:55:29 -0700

Hi Marc/Paul,

I am using latest GF b55 with jersey 0.2. Using latest REST tooling
source, I am seeing the following in generated wadl
file.

<application xmlns="http://research.sun.com/wadl/2006/10">
    <resources base="%%REPLACE%%/resources">
        <resource path="/customers/">
            <method name="GET">
                <response>
                    <representation/>
                </response>
            </method>
            <method name="GET">
                <response>
                    <representation/>
                </response>
            </method>
...
instead of

<application xmlns="http://research.sun.com/wadl/2006/10">
    <resources base="%%REPLACE%%/resources">
        <resource path="/customers/">
            <method name="GET">
                <response>
                    <representation mediatype="application/xml"/>
                </response>
            </method>
            <method name="GET">
                <response>
                    <representation mediatype="application/json"/>
                </response>
            </method>
...

This is same for POST too.

Attached is the application.wadl and the CustomerResource.java

Thanks
Ayub


/*
 * CustomerResource
 *
 * Created on July 17, 2007, 1:30 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package test.service;

import javax.ws.rs.UriTemplate;
import javax.ws.rs.UriParam;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.core.HttpContext;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.WebApplicationException;
import javax.persistence.NoResultException;
import test.Customer;
import test.PurchaseOrder;
import test.DiscountCode;
import java.util.Collection;
import test.converter.CustomerConverter;

/**
 *
 * @author Owner
 */

public class CustomerResource {
    @HttpContext
    private UriInfo context;
    
    /** Creates a new instance of CustomerResource */
    public CustomerResource() {
    }

    /**
     * Constructor used for instantiating an instance of dynamic resource.
     *
     * @param context HttpContext inherited from the parent resource
     */
public CustomerResource(UriInfo context) {
        this.context = context;
    }

    /**
     * Get method for retrieving an instance of Customer identified by id in XML format.
     *
     * @param id identifier for the entity
     * @return an instance of CustomerConverter
     */
@HttpMethod("GET")
    @ProduceMime("application/xml")
    public CustomerConverter getXml(@UriParam("customerId")
    Integer id) {
        try {
            return new CustomerConverter(getEntity(id), context.getURI());
        } finally {
            PersistenceService.getInstance().close();
        }
    }

    /**
     * Get method for retrieving an instance of Customer identified by id in XML format.
     *
     * @param id identifier for the entity
     * @return an instance of CustomerConverter
     */
@HttpMethod("GET")
    @ProduceMime("application/json")
    public CustomerConverter getJson(@UriParam("customerId")
    Integer id) {
        try {
            return new CustomerConverter(getEntity(id), context.getURI());
        } finally {
            PersistenceService.getInstance().close();
        }
    }

    /**
     * Put method for updating an instance of Customer identified by id using XML as the input format.
     *
     * @param id identifier for the entity
     * @param data an CustomerConverter entity that is deserialized from a XML stream
     */
@HttpMethod("PUT")
    @ConsumeMime("application/xml")
    public void putXml(@UriParam("customerId")
    Integer id, CustomerConverter data) {
        PersistenceService service = PersistenceService.getInstance();
        try {
            service.beginTx();
            updateEntity(getEntity(id), data.getEntity());
            service.commitTx();
        } finally {
            service.close();
        }
    }

    /**
     * Put method for updating an instance of Customer identified by id using XML as the input format.
     *
     * @param id identifier for the entity
     * @param data an CustomerConverter entity that is deserialized from a XML stream
     */
@HttpMethod("PUT")
    @ConsumeMime("application/json")
    public void putJson(@UriParam("customerId")
    Integer id, CustomerConverter data) {
        PersistenceService service = PersistenceService.getInstance();
        try {
            service.beginTx();
            updateEntity(getEntity(id), data.getEntity());
            service.commitTx();
        } finally {
            service.close();
        }
    }

    /**
     * Delete method for deleting an instance of Customer identified by id.
     *
     * @param id identifier for the entity
     */
@HttpMethod("DELETE")
    public void delete(@UriParam("customerId")
    Integer id) {
        PersistenceService service = PersistenceService.getInstance();
        try {
            service.beginTx();
            Customer entity = getEntity(id);
            service.removeEntity(entity);
            service.commitTx();
        } finally {
            service.close();
        }
    }

    /**
     * Returns a dynamic instance of DiscountCodeResource used for entity navigation.
     *
     * @param id identifier for the parent entity
     * @return an instance of DiscountCodeResource
     */
@UriTemplate("discountCode/")
    public DiscountCodeResource getDiscountCodeResourceForDiscountCode(@UriParam("customerId")
    Integer id) {
        final Customer parent = getEntity(id);
        return new DiscountCodeResource(context) {

            @Override
            protected DiscountCode getEntity(String id) {
                DiscountCode entity = parent.getDiscountCode();
                if (entity == null) {
                    throw new WebApplicationException(new Throwable("Resource for " + context.getURI() + " does not exist."), 404);
                }
                return entity;
            }
        };
    }

    /**
     * Returns a dynamic instance of PurchaseOrdersResource used for entity navigation.
     *
     * @param id identifier for the parent entity
     * @return an instance of PurchaseOrdersResource
     */
@UriTemplate("purchaseOrders/")
    public PurchaseOrdersResource getPurchaseOrdersResourceForPurchaseOrderCollection(@UriParam("customerId")
    Integer id) {
        final Customer parent = getEntity(id);
        return new PurchaseOrdersResource(context) {

            @Override
            protected Collection<PurchaseOrder> getEntities() {
                return parent.getPurchaseOrderCollection();
            }

            @Override
            protected void createEntity(PurchaseOrder entity) {
                super.createEntity(entity);
                entity.setCustomerId(parent);
            }
        };
    }

    /**
     * Returns an instance of Customer identified by id.
     *
     * @param id identifier for the entity
     * @return an instance of Customer
     */
protected Customer getEntity(Integer id) {
        try {
            return (Customer) PersistenceService.getInstance().createNamedQuery("Customer.findByCustomerId").setParameter("customerId", id).getSingleResult();
        } catch (NoResultException ex) {
            throw new WebApplicationException(new Throwable("Resource for " + context.getURI() + " does not exist."), 404);
        }
    }

    /**
     * Updates entity using data from newEntity.
     *
     * @param entity the entity to update
     * @param newEntity the entity containing the new data
     * @return the updated entity
     */
protected Customer updateEntity(Customer entity, Customer newEntity) {
        newEntity.setCustomerId(entity.getCustomerId());
        entity.getPurchaseOrderCollection().removeAll(newEntity.getPurchaseOrderCollection());
        for (PurchaseOrder value : entity.getPurchaseOrderCollection()) {
            value.setCustomerId(null);
        }
        entity = PersistenceService.getInstance().mergeEntity(newEntity);
        for (PurchaseOrder value : entity.getPurchaseOrderCollection()) {
            value.setCustomerId(entity);
        }
        return entity;
    }
}