CS639 firstRest Project from www.vogella.de

---------------Hello.java: the server code (in part)-------------------

package cs639.first;

 

    import ...

 

    //Sets the path to base URL + /hello

    @Path("/hello")

    public class Hello {

       

 

        // This method is called if XML is request

        @GET

        @Produces(MediaType.TEXT_XML)

        public String sayXMLHello() {

            return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";

        }

 

        // This method is called if HTML is request

        @GET

        @Produces(MediaType.TEXT_HTML)

        public String sayHtmlHello() {

            return "<html> " + "<title>" + "Hello Jersey" + "</title>"

                    + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";

        }

       

        // this echoes received XML elements inside an echo element

        @POST

        @Consumes(MediaType.TEXT_XML)

        @Produces(MediaType.TEXT_XML)

        public String echoXMLHello(String message) {

            int endDeclaration = message.indexOf("?>");       

            return message.substring(0,endDeclaration+2) +

                "<echo>" + message.substring(endDeclaration + 2) + "</echo>";

        }

    }

--------------------------------build.xml-----------------------------------

<project name="firstRest">

 

    <property name="source.dir" value="src" />

    <property name="webcontent.dir" value="WebContent/WEB-INF" />

    <property name="classes.dir" value="${webcontent.dir}/classes" />

    <property name="lib.dir" value="${webcontent.dir}/lib" />

 

    <property environment="env" />

    <!-- on PC, maybe c:\tomcat-6.0, on UNIX, /home/username/cs636/tomcat-6.0 -->

    <property name="CATALINA_HOME" value="${env.CATALINA_HOME}" />

    <!-- on PC, http://localhost:8080, on UNIX http://sf06.cs.umb.edu:xxxxx  -->

    <!-- Note that this env var is also accessed from EndToEndTest.java -->

    <property name="TOMCAT_URL" value="${env.TOMCAT_URL}" />

    <!--base URL for REST services -->

    <property name="BASE_URL" value="${TOMCAT_URL}/${ant.project.name}/rest/" />

    <property name="deploy.dir" value="${CATALINA_HOME}/webapps/${ant.project.name}" />

 

    <path id="project.classpath">

        <pathelement location="${classes.dir}" />

        <fileset dir="${lib.dir}" includes="*.jar" />

    </path>

    <target name="init">

        <mkdir dir="${classes.dir}" />

    </target>

    <target name="clean">

        <delete dir="${classes.dir}" />

    </target>

    <target name="build" depends="init">

        <echo message="${ant.project.name}: ${ant.file}" />

        <javac destdir="${classes.dir}" debug="on">

            <src path="src" />

            <classpath refid="project.classpath" />

        </javac>

    </target>

    <target name="runClient" depends="build">

        <echo message="Running client Test. Note it uses env var TOMCAT_URL" />

        <java fork="yes" classname="cs639.first.client.Test" failonerror="true">

            <classpath refid="project.classpath" />

        </java>

    </target>

   

    <!--use wget to to HTTP GET with accept headers to request various formats-->

    <target name="restGetHTML">

        <echo message="running wget to get html" />

        <exec executable="wget">

            <arg value="--header=Accept: text/html" />

            <arg value="${BASE_URL}hello" />

            <arg value="-Ohello-response.html" />

        </exec>

    </target>

 

    <target name="restGetXML">

        <echo message="running wget to get XML" />

        <exec executable="wget">

            <arg value="--header=Accept: text/xml" />

            <arg value="${BASE_URL}hello" />

            <arg value="-Ohello-response.xml" />

        </exec>

    </target>

    <target name="restEcho">

            <echo message="running wget to post order to /order" />

            <exec executable="wget">

                <arg value="--post-file=order.xml"/>

                <arg value="--header=Content-Type: text/xml" />

                <arg value="${BASE_URL}hello" />

                <arg value="-Oecho-response.xml"/>

            </exec>

</project>

------------------------------------------web.xml-----------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>de.vogella.jersey.first</display-name>

  <servlet>

    <servlet-name>Jersey REST Service</servlet-name>

    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <init-param>

      <param-name>com.sun.jersey.config.property.packages</param-name>

      <param-value>cs639.first</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>Jersey REST Service</servlet-name>

    <url-pattern>/rest/*</url-pattern>

  </servlet-mapping>

</web-app>