无法从泽西响应中获取正确的json值

时间:2015-12-31 11:32:46

标签: java json jersey jersey-2.0

我从数据库中获取值,我有一个webservice,它返回从数据库中获取的值列表,我将它发送到前端视图层,请求来自角度,如webservice调用

问题是我得到的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中的问题比在前端的问题但是没有弄清楚出了什么问题

3 个答案:

答案 0 :(得分:2)

你有

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

答案 1 :(得分:1)

当你正在服用&#34; JOB_PRIMARY_SKILL&#34;从结果集使用rs.getString,试试 rs.getString(&#34; JOB_DESIGNATION&#34;),也许你错过了。

答案 2 :(得分:0)

您的错误来自此处:`

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

它应该是:

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

`