Your First Cup: An Introduction to the Java EE Platform

Creating the Endpoint

In NetBeans 6.7 IDE, create a web project with a Java class source file called DukesAgeResource.java in the firstcup.webservice package using the RESTful Web Service wizard.

ProcedureCreate the Project in NetBeans

  1. Select File->New Project.

  2. Select Java Web in the Categories pane.

  3. Select Web Application in the Projects pane.

  4. Click Next.

  5. Set Project Name to dukes-age.

  6. Set the Project Location to tut-install/myexample.

  7. Click Next.

  8. Select your Enterprise Server v3 instance from the Server menu.

  9. Select Java EE 5 from the Java EE Version menu.


    Note –

    NetBeans 6.7 IDE doesn't currently support Java EE 6 as a Java EE Version, but our application uses Java EE 6 features, and will run on Enterprise Server v3, a Java EE 6 server.


  10. Set the Context Path to /DukesAgeService

  11. Click Finish.

    You should now see the module you created in the Projects pane.

  12. From the Projects pane, right-click on the index.jsp file and select Delete. Click Yes in the dialog.

ProcedureCreate the DukesAgeResource Class

  1. Make sure dukes-age is selected in the Project menu.

  2. Select File->New File.

  3. Select Web Services in the Categories pane.

  4. Select RESTful Web Services From Patterns in the File Types pane.

  5. Click Next.

  6. Under Select Pattern select Singleton and click Next.

  7. Set Resource Package to firstcup.webservice.

  8. Under Path enter dukesAge.

  9. Under Class Name enter DukesAgeResource.

  10. Under MIME Type select text/plain.

  11. Click Finish.

    You should now see the DukesAgeResource.java file inside the firstcup.webservice package in the Projects pane. The DukesAgeResource.java file should also be open in the editor pane.

ProcedureConfigure the dukes-age Web Application

By default, NetBeans 6.7 IDE bundles the JAX-RS 1.0 JARs with web applications that use JAX-RS 1.0. Enterprise Server v3 already has the JAX-RS 1.0 JARs in the server classpath, so there is no need to separately include the JARs.

The default URL that is brought up in a web browser when you run dukes-age can also be configured in NetBeans 6.7 IDE.

  1. Right-click on dukes-age in the Projects tab and select Properties.

  2. Click Libraries.

  3. Uncheck the boxes under Compile-time Libraries for JAX-RS 1.0 and Jersey 1.0 (JAX-RS RI).

  4. Click Run.

  5. Set Relative URL to /resources/dukesAge.

  6. Click OK.

ProcedureRemove the putText Method

The DukesAgeResource JAX-RS resource doesn't respond to HTTP PUT requests. Delete the generated putText method in DukesAgeResource.

  1. Highlight the following generated Javadoc and method definition and delete it.

    /**
     * PUT method for updating or creating an instance of DukesAgeResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("text/plain")
    public void putText(String content) {
    }

ProcedureImplement the getText Method

Add code to DukesAgeResource.getText that calculates Duke's age at the time of the request. To do this, use the java.util.Calendar and java.util.GregorianCalendar classes to create an object representing the date May 23, 1995, Duke's birthday. Then create another Calendar object representing today's date, and subtract today's year from Duke's birth year. If today's date falls before May 23, subtract a year from this result. Then return the result as a String representation.

  1. Highlight the current code in getText and replace it with the following code:

    // Create a new Calendar for Duke's birthday
    Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
    // Create a new Calendar for today
    Calendar now = Calendar.getInstance();
    
    // Subtract today's year from Duke's birth year, 1995
    int dukesAge = now.get(Calendar.YEAR) - dukesBirthday.get(Calendar.YEAR);
    dukesBirthday.add(Calendar.YEAR, dukesAge);
    
    // If today's date is before May 23, subtract a year from Duke's age
    if (now.before(dukesBirthday)) {
    	dukesAge--;
    }
    // Return a String representation of Duke's age
    return new String("" + dukesAge);
  2. Right-click in the editor window and select Format.

ProcedureResolve the Import Statements

  1. Right-click in the Editor.

  2. Select Fix Imports.

  3. Select File->Save from the menu to save the file.