Loading
Print
tapestry-resteasy guide
beta - used in production

org.tynamo:tapestry-resteasy:0.2.0

Works with RestEASY 2.0-beta-2

(for 0.0.1 release see tapestry-resteasy-0.0.1 guide)

User guide for Tapestry-resteasy module

tapestry-resteasy provides integration with JBoss' RESTEasy, an implementation JAX-RS (Java API for RESTful Web Services). JAX-RS is a very nice little spec and if you know what it is about, the following should be very easy to understand. Otherwise, read up on it at Jersey's overview (the reference implementation) or the actual specification. JBoss' RESTEasy provides a few more features over the reference implementation.

Add the tapestry-resteasy dependency to your pom.xml

To use tapestry-resteasy (an independent module of Tynamo), you need to add the following dependency to your pom.xml:

        <dependency>
            <groupId>org.tynamo</groupId>
            <artifactId>tapestry-resteasy</artifactId>
            <version>0.2.0</version>
        </dependency>

No need to edit your web.xml.

The new tapestry-resteasy integrates with the Tapestry's HttpServletRequest pipeline.

Create a package named "rest" for your rest services.

tapestry-resteasy will scan the "rest" package and automatically add singleton resources to the configuration.

If you want to add extra packages, besides InternalConstants.TAPESTRY_APP_PACKAGE_PARAM + ".rest", to be scanned for annotated REST resource classes you can contribute your package names to the ResteasyPackageManager.

	public static void contributeResteasyPackageManager(Configuration<String> configuration)
	{
		configuration.add("org.tynamo.resteasy.ws");
	}

Code your singleton resource.

Good practices:

  • Name the resource class using the entity name followed by the word "Resource".
  • The @Path should be the entity name in lowercase.
package org.tynamo.examples.simple.rest;

import org.tynamo.examples.simple.MyDomainObject;
import org.tynamo.services.PersistenceService;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.List;

@Path("/mydomainobject")
public class MyDomainObjectResource
{

	private PersistenceService persistenceService;

	public MyDomainObjectResource(PersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	@GET
	@Produces("application/json")
	public List<MyDomainObject> getAllDomains()
	{
		return (persistenceService.getInstances(MyDomainObject.class));
	}

	@POST
	@Consumes("application/json")
	public Response post(MyDomainObject domainObject)
	{
		persistenceService.save(domainObject);
		return Response.ok().build();
	}

	@GET
	@Path("{id}")
	@Produces("application/json")
	public MyDomainObject getDomainObject(@PathParam("id") Long id)
	{
		MyDomainObject domainObject = persistenceService.getInstance(MyDomainObject.class, id);
		if (domainObject == null)
		{
			throw new WebApplicationException(Response.Status.NOT_FOUND);
		}
		return domainObject;
	}

}

Annotate your entities.

Don't forget to annotate your entities with JAXB annotations. You need at least @XmlRootElement. See: http://www.caucho.com/resin-3.1/doc/jaxb-annotations.xtp

package org.tynamo.examples.simple.entities;

import org.hibernate.validator.NotNull;
import org.tynamo.descriptor.annotation.PropertyDescriptor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@XmlRootElement(name = "mydomainobject")
public class MyDomainObject
{
	private Long id;

	private String name;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@PropertyDescriptor(index = 0)
	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	@NotNull(message = "name can't be null")
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public boolean equals(Object o)
	{
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;

		MyDomainObject that = (MyDomainObject) o;

		return getId() != null ? getId().equals(that.getId()) : that.getId() == null;
	}

	public int hashCode()
	{
		return (getId() != null ? getId().hashCode() : 0);
	}

	public String toString()
	{
		return getName();
	}
}

Add a provider library to your pom.xml

By default tapestry-resteasy does not includes any marshaller/unmarshaller dependency.

You will need to manually add the dependency to your pom.xml

The previous examples use a JSON marshaller/unmarshaller provided by the resteasy-jettison-provider library.

<dependency>
	<groupId>org.jboss.resteasy</groupId>
	<artifactId>resteasy-jettison-provider</artifactId>
	<version>2.0-beta-2</version>
</dependency>

Contribute singleton resources

If the rest services autodiscovery is not enough for you, you can manually contribute singleton resources in your AppModule:

	/**
	 * Contributions to the RESTeasy main Application.
	 */
	public static void contributeApplication(Configuration<Object> singletons, ObjectLocator locator)
	{
		singletons.add(locator.autobuild(MyDomainObjectResource.class));
	}

Notes:

If you get this exception:

Caused by: java.lang.ClassCastException: com.sun.xml.stream.ZephyrParserFactory cannot be cast to org.codehaus.stax2.XMLInputFactory2

Add this to your system properties:

javax.xml.stream.XMLInputFactory=com.ctc.wstx.stax.WstxInputFactory

like so:

mvn jetty:run
<!-- Run the application using "mvn jetty:run" -->
<plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>maven-jetty-plugin</artifactId>
	<configuration>
		<!-- Log to the console. -->
		<requestLog implementation="org.mortbay.jetty.NCSARequestLog">
			<!-- This doesn't do anything for Jetty, but is a workaround for a Maven bug
				 that prevents the requestLog from being set. -->
			<append>true</append>
		</requestLog>
		<systemProperties>
			<systemProperty>
				<name>tapestry.compress-whitespace</name>
				<value>false</value>
			</systemProperty>
			<systemProperty>
				<name>tapestry.production-mode</name>
				<value>false</value>
			</systemProperty>
			<systemProperty>
				<name>javax.xml.stream.XMLInputFactory</name>
				<value>com.ctc.wstx.stax.WstxInputFactory</value>
			</systemProperty>
		</systemProperties>
	</configuration>
</plugin>

Check google for more information: http://www.google.com/search?hl=en&q=com.sun.xml.stream.ZephyrParserFactory+cannot+be+cast+to+org.codehaus.stax2.XMLInputFactory2&aq=f&oq=&aqi=

Powered by Atlassian Confluence