我写了以下方法来创建REST API
。有一个例子,
Example_bean.java
public class Example_bean
{
private int idAdress;
private int idPatientt;
private String addressType;
private String line1;
private String line2;
private String city;
private String state;
private String zipCode;
private String country;
private Timestamp dateCreated;
private Timestamp lastUpdated;
/**
* @return the idAdress
*/
public int getIdAdress()
{
return idAdress;
}
/**
* @param idAdress the idAdress to set
*/
public void setIdAdress(int idAdress)
{
this.idAdress = idAdress;
}
/**
* @return the idPatientt
*/
public int getIdPatientt()
{
return idPatientt;
}
/**
* @param idPatientt the idPatientt to set
*/
public void setIdPatientt(int idPatientt)
{
this.idPatientt = idPatientt;
}
/**
* @return the addressType
*/
public String getAddressType()
{
return addressType;
}
/**
* @param addressType the addressType to set
*/
public void setAddressType(String addressType)
{
this.addressType = addressType;
}
/**
* @return the line1
*/
public String getLine1()
{
return line1;
}
/**
* @param line1 the line1 to set
*/
public void setLine1(String line1)
{
this.line1 = line1;
}
/**
* @return the line2
*/
public String getLine2()
{
return line2;
}
/**
* @param line2 the line2 to set
*/
public void setLine2(String line2)
{
this.line2 = line2;
}
/**
* @return the city
*/
public String getCity()
{
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city)
{
this.city = city;
}
/**
* @return the state
*/
public String getState()
{
return state;
}
/**
* @param state the state to set
*/
public void setState(String state)
{
this.state = state;
}
/**
* @return the zipCode
*/
public String getZipCode()
{
return zipCode;
}
/**
* @param zipCode the zipCode to set
*/
public void setZipCode(String zipCode)
{
this.zipCode = zipCode;
}
/**
* @return the country
*/
public String getCountry()
{
return country;
}
/**
* @param country the country to set
*/
public void setCountry(String country)
{
this.country = country;
}
/**
* @return the dateCreated
*/
public Timestamp getDateCreated()
{
return dateCreated;
}
/**
* @param dateCreated the dateCreated to set
*/
public void setDateCreated(Timestamp dateCreated)
{
this.dateCreated = dateCreated;
}
/**
* @return the lastUpdated
*/
public Timestamp getLastUpdated()
{
return lastUpdated;
}
/**
* @param lastUpdated the lastUpdated to set
*/
public void setLastUpdated(Timestamp lastUpdated)
{
this.lastUpdated = lastUpdated;
}
}
Example_table.java
public class Example_table
{
public String insertExample(int idPatient, String addressType, String line1, String line2, String city, String state, String zipCode, String country, Timestamp dateCreated, Timestamp lastUpdated)
{
String result = "";
PreparedStatement ps = null;
Connection con = null;
String sql = "INSERT INTO `Example`(idPatient,AddressType,Line1,Line2,City,State,ZipCode,Country,DateCreated,LastUpdated) VALUES(?,?,?,?,?,?,?,?,?,?)";
try
{
con = DBMaster.getInstance().getConnection();
con.setAutoCommit(false);
ps = con.prepareStatement(sql.toLowerCase());
ps.setInt(1, idPatient);
ps.setString(2, addressType);
ps.setString(3, line1);
ps.setString(4, line2);
ps.setString(5, city);
ps.setString(6, state);
ps.setString(7, zipCode);
ps.setString(8, country);
ps.setTimestamp(9, dateCreated);
ps.setTimestamp(10, lastUpdated);
int executeUpdate = ps.executeUpdate();
con.commit();
if (executeUpdate > 0)
{
result = Common.SAVE_SUCCESS;
} else
{
result = Common.SAVE_ROLLBACK;
}
} catch (Exception ex)
{
try
{
con.rollback();
ex.printStackTrace();
result = Common.SAVE_ROLLBACK;
} catch (SQLException e)
{
e.printStackTrace();
}
} finally
{
try
{
if (ps != null)
{
ps.close();
}
if (con != null)
{
con.close();
}
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
return result;
}
public String editExample(String addressType, String line1, String line2, String city, String state, String zipCode, String country, Timestamp dateCreated, Timestamp lastUpdated, int idAddress)
{
String result = "";
PreparedStatement ps = null;
Connection con = null;
String sql = "UPDATE Example SET AddressType=?,Line1=?,Line2=?,City=?,State=?,ZipCode=?,Country=?,DateCreated=?,LastUpdated=? WHERE idAddress=?";
try
{
con = DBMaster.getInstance().getConnection();
con.setAutoCommit(false);
ps = con.prepareStatement(sql.toLowerCase());
ps.setString(1, addressType);
ps.setString(2, line1);
ps.setString(3, line2);
ps.setString(4, city);
ps.setString(5, state);
ps.setString(6, zipCode);
ps.setString(7, country);
ps.setTimestamp(8, dateCreated);
ps.setTimestamp(9, lastUpdated);
ps.setInt(10, idAddress);
int executeUpdate = ps.executeUpdate();
con.commit();
if (executeUpdate > 0)
{
result = Common.SAVE_SUCCESS;
} else
{
result = Common.SAVE_ROLLBACK;
}
} catch (Exception ex)
{
try
{
con.rollback();
ex.printStackTrace();
result = Common.SAVE_ROLLBACK;
} catch (SQLException e)
{
e.printStackTrace();
}
} finally
{
try
{
if (ps != null)
{
ps.close();
}
if (con != null)
{
con.close();
}
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
return result;
}
public Example_bean getExample(int idAddress)
{
PreparedStatement ps = null;
Connection con = null;
ResultSet rs = null;
Example_bean address_bean = new Example_bean();
String sql = "SELECT * FROM Example WHERE idAddress=?";
try
{
con = DBMaster.getInstance().getConnection();
ps = con.prepareStatement(sql.toLowerCase());
ps.setInt(1, idAddress);
rs = ps.executeQuery();
if (rs.isBeforeFirst())
{
while (rs.next())
{
address_bean.setIdAdress(rs.getInt("idAddress"));
address_bean.setIdPatientt(rs.getInt("idPatient"));
address_bean.setAddressType(rs.getString("AddressType"));
address_bean.setLine1(rs.getString("Line1"));
address_bean.setLine2(rs.getString("Line2"));
address_bean.setCity(rs.getString("City"));
address_bean.setState(rs.getString("State"));
address_bean.setZipCode(rs.getString("ZipCode"));
address_bean.setCountry(rs.getString("Country"));
address_bean.setDateCreated(rs.getTimestamp("DateCreated"));
address_bean.setLastUpdated(rs.getTimestamp("LastUpdated"));
}
}
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
try
{
if (rs != null)
{
rs.close();
}
if (ps != null)
{
ps.close();
}
if (con != null)
{
con.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
return address_bean;
}
public void syncExample(List<Address_bean> beanList)
{
PreparedStatement ps = null;
Connection con = null;
ResultSet rs = null;
try
{
con = DBMaster.getInstance().getConnection();
for (int i = 0; i < beanList.size(); i++)
{
String sql = "SELECT * FROM Example WHERE idAddress=? AND DateCreated=?";
ps = con.prepareStatement(sql.toLowerCase());
Example_bean address_bean = beanList.get(i);
ps.setInt(1, address_bean.getIdAdress());
ps.setTimestamp(2, address_bean.getDateCreated());
rs = ps.executeQuery();
if (rs.isBeforeFirst())
{
while (rs.next())
{
System.out.println("There are records");
editAddress(address_bean.getAddressType(), address_bean.getLine1(), address_bean.getLine2(), address_bean.getCity(), address_bean.getState(),
address_bean.getZipCode(), address_bean.getCountry(), address_bean.getDateCreated(), address_bean.getLastUpdated(), address_bean.getIdAdress());
}
} else
{
System.out.println("There are no records");
insertAddress(address_bean.getIdPatientt(), address_bean.getAddressType(), address_bean.getLine1(), address_bean.getLine2(), address_bean.getCity(), address_bean.getState(),
address_bean.getZipCode(), address_bean.getCountry(), address_bean.getDateCreated(), address_bean.getLastUpdated());
}
}
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
try
{
if (rs != null)
{
rs.close();
}
if (ps != null)
{
ps.close();
}
if (con != null)
{
con.close();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
ExampleJSONService.java
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
@Path("/example")
public class ExampleJSONService
{
@POST
@Path("/syncExample")
@Consumes(MediaType.APPLICATION_JSON)
public String syncExample(List<Example_bean> beanList)
{
System.out.println("Executed Bean List");
for (int i = 0; i < beanList.size(); i++)
{
System.out.println("Inside REST: " + beanList.get(i).getLine1());
}
Example_table address_table=new Example_table();
address_table.syncExample(beanList);
return "";
}
}
TestClass.java
public static void main(String[] args)
{
try
{
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/DuctorEHR_REST/rest/example").path("/syncExample");
Example_table address_table=new Address_table();
Example_bean address_bean = address_table.getExample(341);
address_bean.setLine1("Piliyandala Road");
address_bean.setDateCreated(Common.getSQLCurrentTimeStamp());
address_bean.setLastUpdated(Common.getSQLCurrentTimeStamp());
List<Example_bean> address_beans=new ArrayList<Example_bean>();
address_beans.add(address_bean);
target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(address_beans, MediaType.APPLICATION_JSON_TYPE));
} catch (Exception e)
{
e.printStackTrace();
}
}
当我运行此TestClass.java
时,它会移至synacExample()
类中的ExampleJSONService
方法。该方法返回字符串,它可能是错误消息或成功消息。
TestClass.java
将执行与此类似的许多方法,但必须逐个执行。例如,syncExample
进程应该完成,并且应该在成功/错误消息移动到syncExample2
的过程之前返回(syncExample2
,但代码中没有提到TestClass.java
,{{1会像这样执行很多)。如何一个接一个地完成这些过程?
任何想法?谢谢。