| Author |
Custom Exception handling in Web services
|
Reena Roy
Greenhorn
Joined: Aug 08, 2008
Posts: 1
|
|
Hi All, I have developed a web service using Apache Aixs 2 using eclipse/Apache tomcat 5.5 server Web service takes 4 parameters and inserts in the Database. But am not able to catch the custom exception which i have defined.I have also implemented Serializable for the custom exception which i have created.I have pasted the code below.Kindly help. Code snippet below Web service try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String dataSourceName = "webservice"; String dbURL = "jdbc dbc:" + dataSourceName; con = DriverManager.getConnection(dbURL, "",""); System.out.println("Got connection"+con); String sqlinsert="INSERT INTO EMP(id,Name,Age,Address,PhoneNumer) VALUES ("+ id+ ",'"+name+ "',"+ age+",'"+ address+ "',"+phoneno+")"; System.out.println("Sql insertion"+ sqlinsert); pre = con.prepareStatement(sqlinsert); result = pre.executeUpdate(); if(result !=0) message = result +"Records Inserted"; else message ="No Records Inserted"; } catch(SQLException sqle){ System.out.println("SQL Exception in Webservice DBInsert :"); sqle.printStackTrace(); throw new MyException("SQLEX001","SQL Exception Caught"); } catch (Exception err) { System.out.println("General Exception in Webservice DBInsert " + err); err.printStackTrace(); message ="Exception while insertion"; throw new MyException("GENEX001","General Exception Caught"); } I have created a Portlet client and have deployed the same in Web sphere portal server 6.0 and the code snippet below:--- Web service Portlet Client try { if(request.getParameter("empId")!=null){ tmp = request.getParameter("empId"); Integer intobj = new Integer(tmp); empid = intobj.intValue(); } if(request.getParameter("empName")!=null){ empName = request.getParameter("empName"); } if(request.getParameter("empAddress")!=null){ empAddress = request.getParameter("empAddress"); } if(request.getParameter("empAge")!=null){ tmp = request.getParameter("empAge"); Integer intobj = new Integer(tmp); empAge = intobj.intValue(); } if(request.getParameter("empPhone")!=null){ tmp = request.getParameter("empPhone"); Integer intobj = new Integer(tmp); empPhone = intobj.intValue(); } InsertEmpInfoInsertEmpInfoSOAP11Port_httpStub stub = new InsertEmpInfoInsertEmpInfoSOAP11Port_httpStub(); InsertData insertobj = new InsertData(); insertobj.setId(empid); insertobj.setName(empName); insertobj.setAge(empAge); insertobj.setAddress(empAddress); insertobj.setPhoneno(empPhone); InsertDataResponse res = stub.insertData(insertobj); System.out.println("output: "+res.get_return()); message = res.get_return(); response.setRenderParameter("output", message); } catch (InsertDataFaultException e) { // TODO Auto-generated catch block System.out.println("DBInsertWSClient:--->Exception in Web Service"); System.out.println("Error Code "+e.getFaultMessage().getMyException().getErrorCode()); System.out.println("Error Description "+e.getFaultMessage().getMyException().getErrorDescription()); e.printStackTrace(); } My custom Exception Class as follows package com.ibm.db; import java.io.Serializable; public class MyException extends Exception implements Serializable{ /** * */ private static final long serialVersionUID = -7384814069415378236L; /** * */ private String errorCode; private String errorDescription; public MyException(String errorCode, String errorDescription){ this.errorCode = errorCode; this.errorDescription = errorDescription; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } public String getErrorDescription() { return errorDescription; } public void setErrorDescription(String errorDescription) { this.errorDescription = errorDescription; } } Regards, Reena
|
 |
 |
|
|
subject: Custom Exception handling in Web services
|
|
|