我是Restful网络服务的新手。我想通过Jersey客户端访问服务器。但我得到500错误。我想要做的是通过客户端提交一个表名,并将表内容检索为JSON。有人可以在这里找到错误吗?
这是我的客户端
public JSONObject loadTable(String tablename) throws ClientProtocolException, IOException {
pathParams.add("tablename", tablename);
JSONObject jsontable = service.path("access").path("loadtable").queryParams(pathParams).get(JSONObject.class);
return jsontable;
}
这是我的服务器端
@Path("/loadtable")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.TEXT_PLAIN)
public JSONObject loadTable(@QueryParam("tablename") String tablename) throws Exception {
List<Pricing> pricing = new ArrayList<Pricing>();
try {
query = c.prepareStatement("select * from '" + tablename + "'");
ResultSet ets_rs = query.executeQuery();
while (ets_rs.next()) {
pricing.add(new Pricing(ets_rs.getString(1), ets_rs.getString(2), ets_rs.getString(3), ets_rs.getString(4), ets_rs.getString(5), ets_rs.getString(6)));
}
query.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
JSONObject jsonTable = JsonConvert.getJsonFromPricing(pricing);
System.out.println(jsonTable);
return jsonTable;
}
Server端的Pricing类将Result set转换为JSON。
这是StackTrace:
Exception in thread "AWT-EventQueue-0" com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8080/ServerETS/resources/access/loadtable?tablename=prices returned a response status of 500
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:563)
at com.sun.jersey.api.client.WebResource.get(WebResource.java:179)
at Link.ClientLink.loadTable(ClientLink.java:60)
at Interfaces.Pricing.loadTable(Pricing.java:112)
at Interfaces.Pricing.<init>(Pricing.java:32)
at Interfaces.MainMenu.panelPricing(MainMenu.java:1026)
at Interfaces.AdminData.btnAdminDataOKActionPerformed(AdminData.java:150)
at Interfaces.AdminData.access$000(AdminData.java:26)
at Interfaces.AdminData$1.actionPerformed(AdminData.java:70)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
这是GlassFish Server Log
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class org.json.simple.JSONObject, and Java type class org.json.simple.JSONObject, and MIME media type application/json was not found
... 33 more
答案 0 :(得分:2)
Jersey无法将您对JSONObject
的回复序列化为JSON。将您的方法更改为以下内容:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response loadTable(@QueryParam("tablename") String tablename) throws Exception {
// ...
Pricing pricing = ...
return Response.ok(pricing).build();
}
使用JAXB Pricing
注释@XmlRootElement
并让JAXB转换为JSON。
答案 1 :(得分:0)
由于您尝试将请求数据作为JSON格式发送,因此您可以将服务器使用的数据类型的MIME类型修改为JSON类型:
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
答案 2 :(得分:0)
按如下方式更改了客户端
public String loadTable(String tablename) throws ClientProtocolException, IOException {
pathParams.add("tablename", tablename);
ClientResponse response = service.path("access").path("loadtable").queryParams(pathParams).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
String responseString = response.getEntity(String.class);
return responseString;
}
和服务器端如下,
@Path("/loadtable")
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Pricing> loadTable(@QueryParam("tablename") String tablename) throws Exception {
List<Pricing> pricing = new ArrayList<Pricing>();
try {
query = c.prepareStatement("select * from " + tablename);
ResultSet ets_rs = query.executeQuery();
while (ets_rs.next()) {
pricing.add(new Pricing(ets_rs.getString(1), ets_rs.getString(2), ets_rs.getString(3), ets_rs.getString(4), ets_rs.getString(5), ets_rs.getString(6)));
}
query.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
c.close();
}
}
return pricing;
}
使用JAXB @XmlRootElement进行带注释的定价... 像魔术一样工作! 感谢@LutzHorn的评论!