Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleSample 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/{user}")
    @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
}

...

<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

...