Java Reference
In-Depth Information
@Path ( "/customers" )
public
public interface
interface CustomerResource
CustomerResource {
@GET
@Produces ( "application/xml" )
public
public String getCustomers ();
@GET
@Produces ( "application/xml" )
@Path ( "{id}" )
public
public String getCustomer ( @PathParam ( "id" ) int
int id );
}
In this example, we first create an interface for our CustomerResource that is annotated with
JAX-RS annotations:
public
public class
class CustomerResourceBean
CustomerResourceBean implements
implements CustomerResource {
public
public String getCustomers () {...}
public
public String getCustomer ( int
int id ) {...}
}
Our Spring bean class, CustomerResourceBean , simply implements the CustomerResource
interface. Although you can opt to not define an interface and use JAX-RS annotations direc-
tly on the bean class, I highly suggest that you use an interface. Interfaces work better in
Spring when you use features like Spring transactions.
Now that we have a bean class, we should declare it within a Spring XML file called spring-
beans.xml (or whatever you want to name the file):
<beans
<beans xmlns= "http://www.springframework.org/schema/beans"
<bean id= "custService"
class= "com.shopping.restful.services.CustomerResourceBean" //>
</beans>
Place this spring-beans.xml file within your WAR's WEB-INF/classes directory or within a
JAR within the WEB-INF/lib directory. For this example, we'll put it in the WEB-INF/classes
directory. We will find this file through a class loader resource lookup later on when we write
our Application class.
Next we write our web.xml file:
<web-app>
<web-app>
<context-param>
<context-param>
<param-name>
<param-name> spring-beans-file </param-name>
</param-name>
<param-value>
<param-value> META-INF/applicationContext.xml </param-value>
</param-value>
Search WWH ::




Custom Search