...
Code Block |
---|
language | java |
---|
title | 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@POST
@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
@POST@DELETE
@Path("/du")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteUser(User us)
// deletes a user if they exist in DB
@POST@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
} |
...
Code Block |
---|
language | java |
---|
title | @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.