[英]Mapping data from database to JSON in Java Rest Web Service using MyBatis
[英]how write Json Web service to fetch coressponding data from from database table in java
這是我的代碼:
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值在查詢字符串中傳遞的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.