我已经像这样创建了一个json数组
[
{
"proj_bugcount":"20",
"pro_id":"1",
"proj_status":"bugs",
"pro_name":"Driver MAnagement System"
},
{
"proj_bugcount":"0",
"pro_id":"3",
"proj_status":"released",
"pro_name":"Hotel MAnagement System"
}
]
使用以下代码
package com.test.databaseservice;
import com.test.entity.DashBoard;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
import org.json.JSONObject;
public class DashboardService {
public static List<JSONObject> findRelatedData(int userId) throws JSONException {
String setBugCount = null;
String proId = null;
String setProject = null;
String setStatus = null;
DashBoard dashboard = new DashBoard();
List<JSONObject> resList = new ArrayList<JSONObject>();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e);
}
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/project_management_system", "root", "");
PreparedStatement ps = con.prepareStatement("SELECT project.proj_status,project.Pro_Name,project.proj_bugcount,project.Pro_Id FROM project_details INNER JOIN project ON project_details.Pro_Id = project.Pro_Id WHERE emp_id = ?");
ps.setInt(1, userId);
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsMeta = rs.getMetaData();
int columnCnt = rsMeta.getColumnCount();
ArrayList<String> columnNames = new ArrayList<String>();
for (int i = 1; i < columnCnt + 1; i++) {
columnNames.add(rsMeta.getColumnName(i).toLowerCase());
}
while (rs.next()) {
JSONObject obj = new JSONObject();
for (int a = 1; a < columnCnt + 1; a++) {
String key = columnNames.get(a - 1);
String value = rs.getString(a);
obj.put(key, value);
}
resList.add(obj);
}
con.close();
} catch (SQLException e) {
System.out.println(e);
}
return resList;
}
}
然后我将它传递给下面的网络服务
package com.test.rest;
import com.test.databaseservice.DashboardService;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import org.json.JSONException;
import org.json.JSONObject;
@Path("dashboard")
public class DashBoardRestServices {
@Path("find/{userName}")
@GET
@Consumes({MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public List<JSONObject> find(@PathParam("userName") int userName) throws JSONException{
//Find Employee from the database with give Id
//DashBoard dashboard = new DashBoard();
DashboardService Dashboardservice = new DashboardService();
List<JSONObject> resList = Dashboardservice.findRelatedData(userName);
System.out.println(resList);
return resList;
}
}
system.out.println正确输出创建的数组。
但是当我导航到网络服务Url时,它显示在Error
下面 No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception,
disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0])
那里有什么问题?