Java Restful Service Tutorial - Connect to a database - Part 3
Java Restful Service Tutorial - Creating a rest service - Part 2
Jersey framework - http://jersey.java.net
Jackson framework - http://jackson.codehaus.org
@Path("/v1/status/*") //it is good practice to conclude the version information in the path
@Path could be nested by declaring in the class level or method level
@GET
@POST used to submit data like Loing pages with HTTPS you can protect the data
@PUT used mainly for updating data but can be used for inserting/adding data
@DELETE
@HEAD used to return meta-data of the resource
@Produces it could be in the class level to be treated as a default
@Consumes 1st, it is used with @POST most commonly; 2nd, you can define your own or used the predufine MediaType
3rd, The body will be brought into the java code as a String called message
Example of Database Status
Objective: to create a URL that will check our database connection
<localhost:7001/console> is the admin Console for Weblogic;
then goto the resource panel and create a new database resource; then note down the JNDI name for the data source;
to write a DAO java file; first new InitialContext(); then context.lookup("JNDI Name");
conn=datasource.getConnection();
query=conn.prepareStatement("select * from dual");
ResultSet rs = query.executeQuery();
while(rs.next()) {rs.getString(DataTIme)}; query.close() which shoudl be in the finally block;
Java Restful Service Tutorial - Short intro video - Part 1
Use HTTP methods explicityly
Expose directory structure- like URIs
Transfer XML JavaScript Object NOnotation or bnoth
be stateless
There are plenty of reasons to use it:
SImpler thatn SOAP
more expressive, HUman readable results
Can send XML JSON
Java Restful Service Tutorial - Creating Search functionality - Part 4
//jsonlint.com is a website to validate the JSON format
import org.codehaus.jettison.json.*
if w have multi rows, we wanti it organized, so it is easy to search through
the JSON come into play
Key Colon Value
{
"PC_PARTS_PK":"primary key"
"PC_PARTS_TITLE":"product name"
"PC_PARTS_CODE":"product ID code"
}
JASON Object Example of Single Row
{
"PC_PARTS_PK":"1"
"PC_PARTS_TITLE":"Intel MB"
"PC_PARTS_CODE":"123456"
}
JASON Object Example of multi Rows using JASON array
{
{
"PC_PARTS_PK":"1"
"PC_PARTS_TITLE":"Intel MB"
"PC_PARTS_CODE":"123456"
},
{
"PC_PARTS_PK":"2"
"PC_PARTS_TITLE":"Intel MB"
"PC_PARTS_CODE":"456789"
}
}
//ToJson.java, this class could be also rewriten to be a static Java class
//why do they throws excpetions
public JSONArray toJSONArrya(ResultSet rs) throws exception
{
JSONArray json=new JSONArray();
rs.getMetaData() is used to get the information regarding column name, row counts etc,;
rs.getMetaData().getColumnCount() is used to get the row counts;
JSONObject obj= new JSONObject();
obj.put(column_name,rs.getObject(column_name))
json.put(obj)
//V1_inventory.java
//The response object(recommanded) is telling Jersey that we want to customize the http response
public Response returnAllPcParts() throws Exception{
//The direct return java type will delegate it to Jersey
public String returnAllPcParts() throws Exception{
ResultSet rs= query.executoQuery();
ToJson converter = new ToJSON();
JSONArray json= new JSONArray();
json=converter.toJSONArray(rs);
Senior Systems Engineer = json.toString();
return Senior Systems Engineer;
This series is about learning and bulding your own converter will helpo you
understand how JSON works and how easy it is to use;
when you use a rdr pary tool, it usually hard to modify it;
if you build your own, it simple to change it when needed.