Reputation: 7008
I am trying to return values fetched from database to frontend view using rest webservice but the problem is its returning only the one line(first line) of json values
I have json like this:
{"jobName":"NASA Scientist","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"Sample 2","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"NASA Scientist","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Web developer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"XYZ 123","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Web developer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Programmer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
{"jobName":"Programmer","jobPrimarySkill":null,"jobRole":"JOB_ROLE","jobDesignation":"JOB_EXP","jobDescription":"JOB_DESCRIPTION","jobSalaryRange":"JOB_POSITIONS","jobExp":"JOB_SAL_RANGE","jobPositions":"JOB_POSTEDBY","jobPostedBy":null}
Its returning only first line of json values to the front end rather than all of them. I am adding the fetched values from database to List
and exposing it in webservice like this:
public class FetchJobSummaryDAO {
public List getJobSummaries() {
JobSummaries jobSummaries = new JobSummaries();
List<JobSummaries> jobSummaryList = new ArrayList<JobSummaries>();
try {
Connection con = DBConnection.getConnection();
String query = "select JOB_NAME,JOB_DESCRIPTION,JOB_ROLE,JOB_PRIMARY_SKILL,JOB_DESIGNATION,JOB_EXP,JOB_SAL_RANGE, JOB_POSTEDBY from TBL_JOBPOSTING";
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription(rs.getString("JOB_DESCRIPTION"));
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation(rs.getString("JOB_DESIGNATION"));
jobSummaries.setJobExp(rs.getString("JOB_EXP"));
jobSummaries.setJobSalaryRange(rs.getString("JOB_SAL_RANGE"));
jobSummaries.setJobPostedBy(rs.getString("JOB_POSTEDBY"));
jobSummaryList.add(jobSummaries);
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) {
System.out.print(", ");
}
String columnValue = rs.getString(i);
System.out.print(rsmd.getColumnName(i) + " " + columnValue);
}
System.out.println("");
}
} catch (SQLException ex) {
Logger.getLogger(FetchJobSummaryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return jobSummaryList;
}
webservice:
@Path("/FetchJobSummary")
public class FetchJobSummaryService {
FetchJobSummaryDAO dao = new FetchJobSummaryDAO();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<JobSummaries> fetch() {
List js = dao.getJobSummaries();
for(int i=0; i< js.size(); i++){
System.out.println(js); //prints values with same hashcode
}
return dao.getJobSummaries();
}
}
When I print the list, it prints values with same hashcode so I guess its getting only one value of json
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
[com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809, com.RTH.WebServices.JobSummaries@691c5809]
Upvotes: 2
Views: 65
Reputation: 734
I think there are two issues: 1) You need to create a new instance each time you loop in getJobSummaries 2) You need to print out each row not the list each time
the issue is here:
List js = dao.getJobSummaries();
for(int i=0; i< js.size(); i++){
System.out.println(js); //prints the whole list every time
}
You're printing out the list every time. I think you want to get the items in the list - like:
List js = dao.getJobSummaries();
for (Object j : js) {
System.out.println(j); //prints each item in the list
}
Upvotes: 1
Reputation: 1575
Change like this
public class FetchJobSummaryDAO {
public List getJobSummaries() {
JobSummaries jobSummaries = null;
List<JobSummaries> jobSummaryList = new ArrayList<JobSummaries>();
try {
Connection con = DBConnection.getConnection();
String query = "select JOB_NAME,JOB_DESCRIPTION,JOB_ROLE,JOB_PRIMARY_SKILL,JOB_DESIGNATION,JOB_EXP,JOB_SAL_RANGE, JOB_POSTEDBY from TBL_JOBPOSTING";
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs = pst.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
jobSummaries = new JobSummaries()
jobSummaries.setJobName(rs.getString("JOB_NAME"));
jobSummaries.setJobDescription(rs.getString("JOB_DESCRIPTION"));
jobSummaries.setJobRole(rs.getString("JOB_ROLE"));
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation(rs.getString("JOB_DESIGNATION"));
jobSummaries.setJobExp(rs.getString("JOB_EXP"));
jobSummaries.setJobSalaryRange(rs.getString("JOB_SAL_RANGE"));
jobSummaries.setJobPostedBy(rs.getString("JOB_POSTEDBY"));
jobSummaryList.add(jobSummaries);
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) {
System.out.print(", ");
}
String columnValue = rs.getString(i);
System.out.print(rsmd.getColumnName(i) + " " + columnValue);
}
System.out.println("");
}
} catch (SQLException ex) {
Logger.getLogger(FetchJobSummaryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return jobSummaryList;
}
Upvotes: 2