Java Restful Service Tutorial - Connect to a database
2013-11-07 22:46
288 查看
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.
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.
相关文章推荐
- Java JDBC - How to connect to Oracle using Service Name instead of SID
- Kafka日志报错:[error] k.m.j.KafkaJMX$ - Failed to connect to service:jmx:rmi:///jndi/rmi://10.1.3.116:-1/jmxrmi java.lang.IllegalArgumentException: requirement failed: No jmx port but jmx polling enabled!
- connect to remote mysql database by the java
- java.lang.RuntimeException: Fail to connect to camera service问题
- java.lang.RuntimeException: Fail to connect to camera service
- Failed to connect sshd on "10.6.x6.29:99"ProxyHTTP:java.io.IOException:proxy error:Service Unavailab
- Ubuntu install mysql-connector-java for java to connect MySQL database
- java.lang.RuntimeException: Fail to connect to camera service
- A program use java to connect DataBase
- SAX used in java to connect to database
- Java - Connect to mysql
- java.lang.ClassCastException: $Proxy17 cannot be cast to *.*.*.service.impl.DeviceService
- mysql进阶(十七)Cannot Connect to Database Server
- Simple guide to Java Message Service (JMS) using ActiveMQ
- Fail to connect to camera service的几种原因和解决方法
- How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- Failed to connect to VMware Lookup Service on VMware vCenter Virtual Appliance
- 异常解决:java.net.ConnectException: Call To Master/127.0.0.1:9000 failed on connection exception
- java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect