gpt4 book ai didi

java - 无法从 Jersey 响应中获取正确的 json 值

转载 作者:行者123 更新时间:2023-11-30 08:06:12 25 4
gpt4 key购买 nike

我正在从数据库中获取值,并且我有一个 Web 服务,它返回从数据库中获取的值列表,我将它发送到前端 View 层,就像 Web 服务调用一样,应角度的要求

问题是我得到的 json 值不正确。它获取键而不是值。当我在 chrome 控制台中看到它时,json 就像:

{"jobName":"NASA Scientist",
"jobPrimarySkill":null,
"jobRole":"JOB_ROLE",
"jobDesignation":"JOB_EXP", // keyname instead of value (Incorrect values)
"jobDescription":"JOB_DESCRIPTION", // keyname instead of value (Incorrect values)
"jobSalaryRange":"JOB_POSITIONS", // keyname instead of value (Incorrect values)
"jobExp":"JOB_SAL_RANGE", // keyname instead of value (Incorrect values)
"jobPositions":"JOB_POSTEDBY", // keyname instead of value (Incorrect values)
"jobPostedBy":null}

我的后端代码是这样的:

@Path("/FetchJobSummary")
public class FetchJobSummaryService {

FetchJobSummaryDAO dao = new FetchJobSummaryDAO();

@GET
@Produces(MediaType.APPLICATION_JSON)
public List fetch() {

System.out.println(dao.getJobSummaries());
// prints object [com.RTH.WebServices.JobSummaries@6d90d6c5,
com.RTH.WebServices.JobSummaries@6d90d6c5,
com.RTH.WebServices.JobSummaries@6d90d6c5,....

return dao.getJobSummaries();
}
}

DAO 类:

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("JOB_DESCRIPTION");
jobSummaries.setJobRole("JOB_ROLE");
jobSummaries.setJobPrimarySkill(rs.getString("JOB_PRIMARY_SKILL"));
jobSummaries.setJobDesignation("JOB_DESIGNATION");
jobSummaries.setJobExp("JOB_EXP");
jobSummaries.setJobSalaryRange("JOB_SAL_RANGE");
jobSummaries.setJobPostedBy("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); //Prints correct values
}
}

} catch (SQLException ex) {
Logger.getLogger(FetchJobSummaryDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return jobSummaryList;
}

我确信它是 java 中某个地方的问题而不是前端中的问题,但没有弄清楚哪里出了问题

最佳答案

你有

jobSummaries.setJobRole("JOB_ROLE");

代替

jobSummaries.setJobRole(rs.getString("JOB_ROLE"));

所以你应该有

    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);

关于java - 无法从 Jersey 响应中获取正确的 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com