neeraj soni
asked on
How to get response value in java api.
Hi everyone this is my first question related to java. I am new in building api in java i am passing the null value in json i want some random value in json response which are come from database my code is working in java code is below on java.
output are come contactcode like this.
Driver loaded
connected
ContactCode: CNT-5959
Status: Insert
it is working fine.
but i want to make api in java how can i do here is my code
@Path("addcontact")
@POST
@Consumes(MediaType.APPLIC ATION_JSON )
@Produces(MediaType.APPLIC ATION_JSON )
public AddContactResponse addContact(String json)
{
System.out.println();
String msg = "";
//String contactcode = "CNT-5974";
AddContactRequest add = null;
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);
if(jsonElement.isJsonObjec t())
{
JsonObject jsonObject = jsonElement.getAsJsonObjec t();
add = new AddContactRequest(jsonObje ct.get("co ntact_type ").getAsSt ring(),jso nObject.ge t("contact _category" ).getAsStr ing(), jsonObject.get("contact_su b_category ").getAsSt ring(), jsonObject.get("contact_ti tle").getA sString(),
jsonObject.get("first_name ").getAsSt ring(), jsonObject.get("middle_nam e").getAsS tring(), jsonObject.get("last_name" ).getAsStr ing(), jsonObject.get("Designatio n").getAsS tring(),
jsonObject.get("mobile").g etAsString (), jsonObject.get("phone").ge tAsString( ), jsonObject.get("birth_date ").getAsSt ring(), jsonObject.get("anniversar y_date").g etAsString (),
jsonObject.get("Email").ge tAsString( ), jsonObject.get("location_i d").getAsI nt(), jsonObject.get("active").g etAsString (), jsonObject.get("contact_co de").getAs String());
}
AddContact addcontact = new AddContact();
msg = addcontact.addContact(add) ;
//String response = "";
if (msg.equals("Insert")) {
//AddContact es = new AddContact();
//System.out.println(es.st ats());
AddContactResponse result = new AddContactResponse();
// return new AddContactResponse(msg , result.getContact_code());
return new AddContactResponse(msg, "");
//return new AddContactResponse(result. setStatus( status), result.setContactcode(cont act_code)) ;
} else if (msg.equals("Update")) {
//response = Utitlity.constructJSON("Su ccess", 200, "Update");
return new AddContactResponse(msg, "");
}
//return response;
return null;
}
request
{
"contact_type" : "Customer",
"contact_category" : "General",
"contact_sub_category" : "All",
"contact_title" : "MRS",
"first_name" : "Neeru",
"middle_name" : "sal",
"last_name" : "singh",
"Designation": "Director",
"mobile" : "7509010490",
"phone": "07882325695",
"birth_date" : "08-20-1994",
"anniversary_date" : "08-25-2012",
"Email" : "neer@gmail.com",
"location_id" : 1,
"active" : "1",
"contact_code" :" ",
"status" : ""
}
response
{
"contactcode": "",
"status": "Insert"
}
i am not receiving contact code thanks in advance.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Types;
import com.pal.api.application.ApplicationAPI.DBConnection;
import Modal.AddContactRequest;
public class AddContact {
public String addContact(AddContactRequest add) {
Connection c= DBConnection.getConn();
//String cc = "";
String result = "";
try {
CallableStatement stmt= c.prepareCall("{call something( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?)}");
stmt.setString(1, add.getContactType());
stmt.setString(2, add.getCategory());
stmt.setString(3, add.getSubCategory());
stmt.setString(4, add.getTitle());
stmt.setString(5, add.getFirstName());
stmt.setString(6, add.getMiddleName());
stmt.setString(7, add.getLastName());
stmt.setString(8, add.getDesignation());
stmt.setString(9, add.getMobile());
stmt.setString(10, add.getPhoneNo());
stmt.setString(11, add.getDOB());
stmt.setString(12, add.getDOA());
stmt.setString(13, add.getEmail());
stmt.setInt(14, add.getLocationID());
stmt.setString(15, add.getActive());
stmt.setString(16, add.getContactCode());
stmt.setString(17, "");
//ResultSet rs = stmt.executeQuery();
stmt.registerOutParameter(16, Types.VARCHAR);
stmt.registerOutParameter(17, Types.VARCHAR);
int rs = stmt.executeUpdate();
if (rs > 0)
{
String contactcode = stmt.getString(16);
contactcode = stmt.getString(16);
System.out.println("ContactCode: "+ contactcode);
result = stmt.getString(17);
System.out.println("Status: "+ result);
return result;
}
else
{
return result;
}
}
catch (Exception e) {
e.printStackTrace();
}
return "{\"msg\":\"Failed to Create Customer\"}";
}
}
output are come contactcode like this.
Driver loaded
connected
ContactCode: CNT-5959
Status: Insert
it is working fine.
but i want to make api in java how can i do here is my code
@Path("addcontact")
@POST
@Consumes(MediaType.APPLIC
@Produces(MediaType.APPLIC
public AddContactResponse addContact(String json)
{
System.out.println();
String msg = "";
//String contactcode = "CNT-5974";
AddContactRequest add = null;
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);
if(jsonElement.isJsonObjec
{
JsonObject jsonObject = jsonElement.getAsJsonObjec
add = new AddContactRequest(jsonObje
jsonObject.get("first_name
jsonObject.get("mobile").g
jsonObject.get("Email").ge
}
AddContact addcontact = new AddContact();
msg = addcontact.addContact(add)
//String response = "";
if (msg.equals("Insert")) {
//AddContact es = new AddContact();
//System.out.println(es.st
AddContactResponse result = new AddContactResponse();
// return new AddContactResponse(msg , result.getContact_code());
return new AddContactResponse(msg, "");
//return new AddContactResponse(result.
} else if (msg.equals("Update")) {
//response = Utitlity.constructJSON("Su
return new AddContactResponse(msg, "");
}
//return response;
return null;
}
request
{
"contact_type" : "Customer",
"contact_category" : "General",
"contact_sub_category" : "All",
"contact_title" : "MRS",
"first_name" : "Neeru",
"middle_name" : "sal",
"last_name" : "singh",
"Designation": "Director",
"mobile" : "7509010490",
"phone": "07882325695",
"birth_date" : "08-20-1994",
"anniversary_date" : "08-25-2012",
"Email" : "neer@gmail.com",
"location_id" : 1,
"active" : "1",
"contact_code" :" ",
"status" : ""
}
response
{
"contactcode": "",
"status": "Insert"
}
i am not receiving contact code thanks in advance.
ASKER
Thanks for the answer can you please describe me? actually i am new on this. how to pass contact code string it is in addContact class.
hi neeraj soni
plz check the code
@Path("addcontact")
@POST
@Consumes(MediaType.APPLIC ATION_JSON )
@Produces(MediaType.APPLIC ATION_JSON )
public AddContactResponse addContact(String json)
{
System.out.println();
String msg = "";
//String contactcode = "CNT-5974";
AddContactRequest add = null;
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);
if(jsonElement.isJsonObjec t())
{
JsonObject jsonObject = jsonElement.getAsJsonObjec t();
add = new AddContactRequest(jsonObje ct.get("co ntact_type ").getAsSt ring(),jso nObject.ge t("contact _category" ).getAsStr ing(), jsonObject.get("contact_su b_category ").getAsSt ring(), jsonObject.get("contact_ti tle").getA sString(),
jsonObject.get("first_name ").getAsSt ring(), jsonObject.get("middle_nam e").getAsS tring(), jsonObject.get("last_name" ).getAsStr ing(), jsonObject.get("Designatio n").getAsS tring(),
jsonObject.get("mobile").g etAsString (), jsonObject.get("phone").ge tAsString( ), jsonObject.get("birth_date ").getAsSt ring(), jsonObject.get("anniversar y_date").g etAsString (),
jsonObject.get("Email").ge tAsString( ), jsonObject.get("location_i d").getAsI nt(), jsonObject.get("active").g etAsString (), jsonObject.get("contact_co de").getAs String(),j sonObject. get("AddCo ntactRespo nse");
}
AddContact addcontact = new AddContact();
msg = addcontact.addContact(add) ;
//String response = "";
if (msg.equals("Insert")) {
//AddContact es = new AddContact();
//System.out.println(es.st ats());
AddContactResponse result = new AddContactResponse();
// return new AddContactResponse(msg , result.getContact_code());
return new AddContactResponse(msg, "");
//return new AddContactResponse(result. setStatus( status), result.setContactcode(cont act_code)) ;
} else if (msg.equals("Update")) {
//response = Utitlity.constructJSON("Su ccess", 200, "Update");
return new AddContactResponse(msg, "");
}
//return response;
return AddContactResponse;
}
plz check the code
@Path("addcontact")
@POST
@Consumes(MediaType.APPLIC
@Produces(MediaType.APPLIC
public AddContactResponse addContact(String json)
{
System.out.println();
String msg = "";
//String contactcode = "CNT-5974";
AddContactRequest add = null;
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(json);
if(jsonElement.isJsonObjec
{
JsonObject jsonObject = jsonElement.getAsJsonObjec
add = new AddContactRequest(jsonObje
jsonObject.get("first_name
jsonObject.get("mobile").g
jsonObject.get("Email").ge
}
AddContact addcontact = new AddContact();
msg = addcontact.addContact(add)
//String response = "";
if (msg.equals("Insert")) {
//AddContact es = new AddContact();
//System.out.println(es.st
AddContactResponse result = new AddContactResponse();
// return new AddContactResponse(msg , result.getContact_code());
return new AddContactResponse(msg, "");
//return new AddContactResponse(result.
} else if (msg.equals("Update")) {
//response = Utitlity.constructJSON("Su
return new AddContactResponse(msg, "");
}
//return response;
return AddContactResponse;
}
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Because you don't do anything with it, other than print it out.
Why don't you make your "addContact" method (the one that works with the database) return your AddContactResponse object, rather than just a String and then you can set the contact code field of that object, and then return that object to the client?