如何编写Json Web服务从java中的数据库表中获取同步数据

时间:2013-11-27 06:59:32

标签: java mysql web-services

这是我的代码:

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import dto.FeedObjects;


public class Project {


    public ArrayList<FeedObjects> GetFeeds(Connection connection) throws Exception
    {
        ArrayList<FeedObjects> feedData = new ArrayList<FeedObjects>();
        try
        {
            //String uname = request.getParameter("uname");
            PreparedStatement ps = connection.prepareStatement("SELECT id,title,description,url FROM website ORDER BY id DESC");
            //ps.setString(1,uname);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                FeedObjects feedObject = new FeedObjects();
                feedObject.SetId(rs.getInt("id"));
                feedObject.setTitle(rs.getString("title"));
                feedObject.setDescription(rs.getString("description"));
                feedObject.setUrl(rs.getString("url"));
                feedData.add(feedObject);
            }
            return feedData;
        }
        catch(Exception e)
        {
            throw e;
        }
    }

}

这个类获取数据库表的数据并以json格式转换:

package webService;

import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import model.ProjectManager;

import com.google.gson.Gson;

import dto.FeedObjects;

@Path("/WebService")
public class FeedService {

    @GET
    @Path("/GetFeeds")
    @Produces("application/json")
    public String feed() {
        String feeds = null;
        try {
            ArrayList<FeedObjects> feedData = null;
            ProjectManager projectManager = new ProjectManager();
            feedData = projectManager.GetFeeds();
            Gson gson = new Gson();
            feeds = gson.toJson(feedData);

        } catch (Exception e) {
            System.out.println("error");
        }
        return feeds;
    }

}

包装模型;

import java.sql.Connection;
import java.util.ArrayList;

import dao.Database;

import dao.Project;
import dto.FeedObjects;

public class ProjectManager {


public ArrayList<FeedObjects> GetFeeds(String id)throws Exception {
    ArrayList<FeedObjects> feeds = null;
    try {
            Database database= new Database();
            Connection connection = database.Get_Connection();
            Project project= new Project();
            feeds=project.GetFeeds(connection);

    } catch (Exception e) {
        throw e;
    }
    return feeds;
}

}

还有一个课我们已经获得了设定值。我能够以Json格式显示所有数据库表值使用此URL但我希望当我传递id:

http://localhost:8080/RESTfulProject/REST/WebService/GetFeeds?id=1  

然后它应该只显示一个对应的名称,标题,网址。我试过用这个

http://www.9lessons.info/2012/10/restful-web-services-json-api.html示例但无法执行此操作请帮助我

1 个答案:

答案 0 :(得分:0)

您必须在@QueryParam("id")方法参数中添加String id feed并获取id的值并根据id的值执行操作并返回JSON字符串。请看下面

   @GET
    @Path("/GetFeeds")
    @Produces("application/json")
    public String feed(@QueryParam("id") String id) {
        String feeds = null;
        try {
            ArrayList<FeedObjects> feedData = null;
            ProjectManager projectManager = new ProjectManager();
            // Modify the GetFeeds method by sending the value of id
            // and prepare feed data based on id
            feedData = projectManager.GetFeeds(id);
            Gson gson = new Gson();
            // create the json data for the feed data of the corresponding id value
            feeds = gson.toJson(feedData);

        } catch (Exception e) {
            System.out.println("error");
        }
        return feeds;

   }

在ProjectManager的GetFeeds方法中,通过传递Connection类型的参数来调用Project类的GetFeeds方法。修改它以添加String类型的参数以从QueryParam传递id的值,然后修改查询,如下所示

SELECT id,title,description,url FROM website where id = 'ID_VALUE_IN_QUERY_PARAM'

希望这能清楚地解释一切。这将返回唯一在查询字符串中传递ID值的行。