In this shortish part, we'll add some interfaces to our application so external users can read the current greetings. Expanding the interfaces so greetings can be added is left as an exercise for the reader

JAX-RS

REST is hip (and now in EE 6 as JAX-RS) so let's throw it in. Add

package com.acme.greetings;

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path(value = "/RESTExport")
public class RESTExporter
{

   @Inject
   GreetingServer greetingServer;

   @GET
   @Path("/greetings")
   @Produces("application/xml")
   public List<Greeting> getGreetings()
   {
      return greetingServer.getGreetings();
   }

}

That fetches the current greetings (notice the injection) from the server and presents them in XML format. To hook up the JAX-RS implementation, RESTEasy, add the following to web.xml

<context-param>
	<param-name>resteasy.scan</param-name>
	<param-value>true</param-value>
</context-param>
<context-param>
	<param-name>resteasy.servlet.mapping.prefix</param-name>
	<param-value>/resteasy</param-value>
</context-param>
<listener>
	<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
	<servlet-name>Resteasy</servlet-name>
	<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>Resteasy</servlet-name>
	<url-pattern>/resteasy/*</url-pattern>
</servlet-mapping>

and the @XMLRootElement annotation to the top of our Greeting class

@Entity
@Audited
@XmlRootElement
public class Greeting

Your greetings should now be available from /Greetings/resteasy/RESTExport/greetings.

JAX-WS

Adding a Web Service is even more simple. Add

package com.acme.greetings;

import java.util.List;

import javax.inject.Inject;
import javax.jws.WebService;

@WebService
public class WebServiceExporter
{
   @Inject
   GreetingServer greetingServer;

   public List<Greeting> getGreetings()
   {
      return greetingServer.getGreetings();
   }

}

That does a similar job as our RESTExporter and then hook it up in web.xml

<servlet>
	<servlet-name>WSExport</servlet-name>
	<servlet-class>com.acme.greetings.WebServiceExporter</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>WSExport</servlet-name>
	<url-pattern>/WebServiceExport/*</url-pattern>
</servlet-mapping>

Hmm. Wonder if you can make it auto-register? Anyway, the WDSL should be viewable from /Greetings/WebServiceExport?wsdl

Conclusion

This was a short one. Partly because setting things up is really straightforward and don't require us to do that many workarounds. Hopefully, once Aslak finishes the Arqullian DBUnit integration (I already heard rumors on JSFUnit integration) I can be back with a more thorough article on testing all parts of the application.


Back to top