gpt4 book ai didi

java - 泽西 RESTful 服务,错误 500

转载 作者:行者123 更新时间:2023-12-02 06:35:36 26 4
gpt4 key购买 nike

我是 Restful Web 服务的新手。我想通过 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;
}

服务器端的Pricing类将结果集转换为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 服务器日志

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

最佳答案

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 @XmlRootElement 注释 Pricing 并让 JAXB 进行到 JSON 的转换。

关于java - 泽西 RESTful 服务,错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19686602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com