这是我的代码:
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;
}
}
还有一类我们已经获得了设定值。我能够使用此 URL 以 Json 格式显示所有数据库表值,但我希望当我传递 id 时:
http://localhost:8080/RESTfulProject/REST/WebService/GetFeeds?id=1
那么它应该只显示id一个对应的名称,标题,url。我尝试过使用这个
http://www.9lessons.info/2012/10/restful-web-services-json-api.html示例但无法执行此操作请帮助我
最佳答案
您必须在 feed
方法参数中添加 @QueryParam("id")
String id
并获取 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 值的行。
关于java - 如何编写Json Web服务从Java中的数据库表中获取相应的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235887/