Link to home
Start Free TrialLog in
Avatar of neeraj soni
neeraj soniFlag for India

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.
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\"}";
	
	}
	

}

Open in new window


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.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_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.isJsonObject())
            {
                  JsonObject jsonObject = jsonElement.getAsJsonObject();            
                  add = new AddContactRequest(jsonObject.get("contact_type").getAsString(),jsonObject.get("contact_category").getAsString(), jsonObject.get("contact_sub_category").getAsString(), jsonObject.get("contact_title").getAsString(),
                              jsonObject.get("first_name").getAsString(), jsonObject.get("middle_name").getAsString(), jsonObject.get("last_name").getAsString(), jsonObject.get("Designation").getAsString(),
                              jsonObject.get("mobile").getAsString(), jsonObject.get("phone").getAsString(), jsonObject.get("birth_date").getAsString(), jsonObject.get("anniversary_date").getAsString(),
                              jsonObject.get("Email").getAsString(), jsonObject.get("location_id").getAsInt(), jsonObject.get("active").getAsString(), jsonObject.get("contact_code").getAsString());
            }
          
            AddContact addcontact = new AddContact();
            msg = addcontact.addContact(add);
            //String response = "";
            if (msg.equals("Insert")) {
                  
                  //AddContact es = new AddContact();
                //System.out.println(es.stats());
                  AddContactResponse result = new AddContactResponse();
             // return new AddContactResponse(msg , result.getContact_code());
                  return new AddContactResponse(msg, "");
              //return new AddContactResponse(result.setStatus(status), result.setContactcode(contact_code));
                  
              } else if (msg.equals("Update")) {
                  //response = Utitlity.constructJSON("Success", 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.
Avatar of mccarl
mccarl
Flag of Australia image

i am not receiving contact code

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?
Avatar of neeraj soni

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.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_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.isJsonObject())
            {
                  JsonObject jsonObject = jsonElement.getAsJsonObject();            
                  add = new AddContactRequest(jsonObject.get("contact_type").getAsString(),jsonObject.get("contact_category").getAsString(), jsonObject.get("contact_sub_category").getAsString(), jsonObject.get("contact_title").getAsString(),
                              jsonObject.get("first_name").getAsString(), jsonObject.get("middle_name").getAsString(), jsonObject.get("last_name").getAsString(), jsonObject.get("Designation").getAsString(),
                              jsonObject.get("mobile").getAsString(), jsonObject.get("phone").getAsString(), jsonObject.get("birth_date").getAsString(), jsonObject.get("anniversary_date").getAsString(),
                              jsonObject.get("Email").getAsString(), jsonObject.get("location_id").getAsInt(), jsonObject.get("active").getAsString(), jsonObject.get("contact_code").getAsString(),jsonObject.get("AddContactResponse");
            }
         
            AddContact addcontact = new AddContact();
            msg = addcontact.addContact(add);
            //String response = "";
            if (msg.equals("Insert")) {
                 
                  //AddContact es = new AddContact();
                //System.out.println(es.stats());
                  AddContactResponse result = new AddContactResponse();
             // return new AddContactResponse(msg , result.getContact_code());
                  return new AddContactResponse(msg, "");
              //return new AddContactResponse(result.setStatus(status), result.setContactcode(contact_code));
                 
              } else if (msg.equals("Update")) {
                  //response = Utitlity.constructJSON("Success", 200, "Update");
                  return new AddContactResponse(msg, "");
                 
              }
           
            //return response;      
            return AddContactResponse;                  
    }
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members 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.