Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Summary

This page details an application that is comprised of multiple maven projects.  It will detail the overall maven project structure, the way the code is structured and webservice ports I have exposed

A web services can transmit any type of text structure but JSON is the most common.  The jackson api can be used to marshal data to and from the wire.  Rather then manipulating strings to and from json format directly, I have created Java classes that have the attributes on them that I want to transmit across the wire.  For example all responses from the server should be in json form so I have created the following class

Sample
package com.celestial.util.objects;

/**
 *
 * @author Selvyn
 */
public class SimpleErrorMessage
{
    private String  message;
    
    public  SimpleErrorMessage( String msg )
    {
        message = msg;
    }
    
    public  String  getMessage()
    {
        return message;
    }
    
    public  void    setMessage( String msg )
    {
        message = msg;
    }
}

When transmitted the result will be marshalled as {"message":"some value"}.

Objects such as these that are marshalled between different service points (clients or servers) have been placed in a maven project called DBankBusinessObjects.  All other projects are dependent on this project.  This project must be compiled and installed into your local maven repo so that dependent projects can locate the build artifact (mvn install).

Maven project hierarchy

Top maven project is a maven module project bringing together the service layer and reusable java artifact - DBankAppllicationRoot

DBankBusinessObjects maven project contains objects that will be marshalled between service points

DBankDBTier_ws_exp maven project (_exp implies exposed) is the service layer that exposes services to the database.  The example code has a rest api to create, find, and update users in the users table

Exposing the services

The next section outlines the way I have exposed the services using the jersey api.

Sample Code
@Path("/user")
public class UserRestServices
{
    private String genPassword()

    @GET
    @Path("/sayhello")
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHelloTest() 
    // Test entry point, call this from url in browser to make sure service up and running

    @GET
    @Path("/cu/{user}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response createUser(@PathParam("user") String userId) 
    // create a user from url in browser, calls __createUser

    @POST
    @Path("/cu")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response createUserFromObject(User usr) 
    // create a user from java marshalled object, calls __createUser
    
    private Response    __createUser( User usr )
    // actually makes call to DB

    @DELETE
    @Path("/du")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteUser(User us)
    // deletes a user if they exist in DB

    @PUT
    @Path("/uu")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateUser(User us)
    // updates a user if they exist in DB

    @GET
    @Path("/fu/{user}/{pwd}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findUser(@PathParam("user") String userId, @PathParam("pwd") String pwd)
    // finds a user if they exist in the DB
}

The URIs I have exposed are

<base uri>/user/cu
<base uri>/user/cu/{userid}
<base uri>/user/fu
<base uri>/user/fu/{userid}/{pwd}
<base uri>/user/uu
<base uri>/user/uu/{userid}
<base uri>/user/du/
<base uri>/user/du/{userid}

The URIs that take a parameter can be used directly from the browser or a HTML type form (this includes javascript)

The URIs that don't have a parameter are used in service to service interactions passing JSON strings.  For each of these interactions, the simplest way to specify the contract is through a Java class.  For example the @POST /cu entry point has the following Java class

@POST /cu entry point - contract
package com.celestial.dbbusinessobjects;

/**
 *
 * @author Selvyn
 */
public class User
{
    private String  userID;
    private String  userPwd;
    
    public  User()
    {}
    
    public  User( String userid, String pwd )
    {
        this.userID = userid;
        this.userPwd = pwd;
    }

    public String getUserID()
    {
        return userID;
    }

    public void setUserID(String itsUserID)
    {
        this.userID = itsUserID;
    }

    public String getUserPwd()
    {
        return userPwd;
    }

    public void setUserPwd(String itsUserPwd)
    {
        this.userPwd = itsUserPwd;
    }
    
    @Override
    public String toString()
    {
        return new StringBuffer("itsUserID:").append(this.userID)
                .append("itsUserPwd:").append(this.userPwd).toString();
    }
}

I know that under strict ReSTful architecture, there should be no hint of the service being provided in the URI naming (in other words avoid rpc style naming conventions), to this end I have mapped eah URI to one and only one HTTP command.

  • No labels