如何使用jersey和json在java中检索restful web服务的有效载荷数据

时间:2014-04-11 03:54:19

标签: java mysql json jersey payload

我正在使用Jersey,Json和mysql在java中进行restful webservice。 通过使用chrome的高级休息客户端的POST方法,我通过有效负载发送json数据。 现在我想在我的java文件中读取json数据怎么做?

我的代码: -

public ArrayList<FeedObjects> GetStudent(Connection connection) throws Exception
{
    ArrayList<FeedObjects> feedStudent = new ArrayList<FeedObjects>();
    try
    {
        //String uname = request.getParameter("uname");
        //PreparedStatement ps = connection.prepareStatement("SELECT book_id,bookname,price,author FROM book ORDER BY book_id DESC");

        PreparedStatement ps1 = connection.prepareStatement("SELECT * FROM student,CollegeMaster where student.c_id=1 AND CollegeMaster.c_id = 1");
        //ps.setString(1,uname);
        //ResultSet rs = ps.executeQuery();
        ResultSet rs1 = ps1.executeQuery();
        while(rs1.next())
        {
            FeedObjects feedObject = new FeedObjects();

            feedObject.setS_id(rs1.getString("s_id"));
            feedObject.setS_name(rs1.getString("s_name"));
            feedObject.setS_address(rs1.getString("s_address"));
            feedObject.setS_contact(rs1.getString("s_contact"));
            feedObject.setS_class(rs1.getString("s_class"));
            feedObject.setC_name(rs1.getString("c_name"));
            feedObject.setC_address(rs1.getString("c_address"));
            feedStudent.add(feedObject);
        }
        return feedStudent;
    }
    catch(Exception e)
    {
        throw e;
    }
}

我有{&#34; c_id&#34;:&#34; 1&#34;}来自高级休息客户端的有效负载(POST方法的有效负载),它位于json中。 现在我想从我的java程序中读取这个c_id然后, 从上面的代码..我想将c_id = 1添加到下面的查询中。 PreparedStatement ps1 = connection.prepareStatement(&#34; SELECT * FROM student,CollegeMaster,student.c_id = 1 AND CollegeMaster.c_id = 1&#34;);

1 个答案:

答案 0 :(得分:0)

您好,这是我的工作示例。

创建这样的第一个动作类,您可以在其中读取json并执行逻辑 任何你想要的。

package com.webapp.actions.api.task;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import com.webapp.models.TaskModel;
import com.webapp.models.UserModel;

@Path("/api/task")
public class TaskAction extends BusinessApiAction {

@POST
@Consumes({ "application/json", "application/xml" })
@Produces({ "application/json", "application/xml" })
public Response insertTask(TaskModel taskModel) throws IOException {


    //you can print values in taskModel (your json)

    // insert u=your logic for work on your json data.  


   }
}

还根据你的json设计模型类 恩。这里

package com.webapp.models;
public class TaskModel  


private String taskId;
private String title;
private String descriptionText;
private boolean completed;
private long dateDue;
private int priority;
private String userId;


public String getTaskId() {
    return taskId;
}

public void setTaskId(String taskId) {
    this.taskId = taskId;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescriptionText() {
    return descriptionText;
}

public void setDescriptionText(String descriptionText) {
    this.descriptionText = descriptionText;
}

public boolean isCompleted() {
    return completed;
}

public void setCompleted(boolean completed) {
    this.completed = completed;
}

public long getDateDue() {
    return dateDue;
}

public void setDateDue(long dateDue) {
    this.dateDue = dateDue;
}

public int getPriority() {
    return priority;
}

public void setPriority(int priority) {
    this.priority = priority;
}

public boolean isAllDay() {
    return allDay;
}


public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}
}

现在你的json看起来像这样

    {
    "auth_token":"95dsfsdf758f75049",
   "taskId":"27878HG",
   "title":"my task",
   "descriptionText":"this is new task",
   "completed":false,
   "dateDue":1393911124000,
   "priority":1,
   "roleId":"GHsT-5sf1-4719-84E6F-8CGFR42189" 
}

和方法

   http://localhost:8080/myproject/api/task.json

和方法是POST() 并添加请求标头内容类型:application / json