我正在尝试使用rest webservice将从数据库获取的值返回到前端视图,但问题是它只返回json值的一行(第一行)
我有这样的json:
{"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}
它只将第一行json值返回到前端而不是全部。我将从数据库中获取的值添加到List
并在Web服务中公开,如下所示:
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;
}
web服务:
@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();
}
}
当我打印列表时,它会打印具有相同哈希码的值,所以我猜它只获得一个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]
答案 0 :(得分:2)
像这样改变
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;
}
答案 1 :(得分:1)
我认为有两个问题: 1)每次在getJobSummaries中循环时都需要创建一个新实例 2)您需要每次打印出不在列表中的每一行
问题在于:
List js = dao.getJobSummaries();
for(int i=0; i< js.size(); i++){
System.out.println(js); //prints the whole list every time
}
您每次都要打印出列表。我想你想得到列表中的项目 - 比如:
List js = dao.getJobSummaries();
for (Object j : js) {
System.out.println(j); //prints each item in the list
}