Flex3 and Rest Webservices.
In our organization since we want leverage Oracle Bpel , i used Flex with Oracle Bpel Webserivces for creating Dashboards and other Rich Internet applications.
Recently i was developing REST webserivces for another project. it was very interesting...As we all know in flex3 we can only use the HTTP service for invoking a REST webservice.
The main reasons why i selected REST instead of BPEL to bind/post data on a flex page is
did not require an ws-security extensions..
did not require soap-header information..
am not willing to select an open source implementation for that project...
Tools required: Ganymede 3.4 and myclispse 7.5 installation (make sure it supports jsr311 implementation).
Framework: Jersey.
use case:
submit a form with firstname and last name and persist in a database
Step 1.
Create a POJO class ,annotate with @XmlRootElement
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee{
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Step 2.
Create a Resource class
package com.prem.restservices;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import com.sun.jersey.spi.resource.Singleton;
@Produces("application/xml")
@Path("empdetails")
@Singleton
public class CustomersResource {
String jdbcURL = "jdbc:oracle:thin:@hostname:port:SID";
String user = "username";
String passwd = "password";
public Connection getConnection() throws SQLException{
Connection conn;
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(jdbcURL, user, passwd);
return conn;
}
public CustomersResource() {
}
@POST
@Path("add")
@Produces("text/plain")
@Consumes("application/xml")
public String addEmployee(Employee employee) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = getConnection();
//Insert Data in your Database
} catch (java.sql.SQLException e) {
// TODO Auto-generated catch block
System.out.println("Exception in ad Employee" +customer.toString());
}finally{
try{
//Release Resources
}catch(Exception e){
//log in exception
}
}
return "employee " + employee.getFirstName() + " added";
}
Flex Code
mx:Script
![CDATA[
private function send_data():void {
userRequest.send();
}
]]
mx:Form x="22" y="10" width="493"
mx:HBox
mx:Label text="FirstName"
mx:TextInput id="firstname"
mx:HBox
mx:HBox
mx:Label text="LastName"
mx:TextInput id="lastname"
mx:HBox
mx:Button label="Submit" click="send_data()"
mx:Form
mx:HTTPService id="userRequest" url="Webservice URL" contentType="application/xml" showBusyCursor="true" useProxy="false"
mx:request xmlns=""
employee
firstName{firstname.text}firstName
lastName{lastname.text}lastName
employee
mx:request
mx:HTTPService
No comments:
Post a Comment