我正在使用restful web service
. 我用过hibernate
和POJO
to mysql communication
。
我有具有用户表的数据库,主要是empid
. 为了获取单行,我将 html 页面中的 empid 传递给 Web 服务,并将 json 对象返回给 html 页面(使用 javascript 进行解析)。
where empid=277;
我已经用这个来从 DB // Hardcoded here中获取单行
new_user = (User) session.get(User.class, (long)277);
我想从其他列中检索记录,例如empidid(不是empid,empid 是主键)
select * user where employeeid="XX-123XD"
我已经通过了employeeid
from html 页面,它是字符串。
我写过网络服务,比如
@POST
@Path("/getjson")
@Produces("application/json")
public JSONObject sendjson(String employeeid) {
Gson gsonobj = new Gson();
JSONObject jsonobj = null;
SessionFactory fact;
fact = new Configuration().configure().buildSessionFactory();
User new_user = null;
Session session = fact.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from user where employeeid= :employee");
query.setParameter("employee", employeeid);
//new_user = (User) session.get(User.class, (long)277);
new_user = (User) query.list();
try {
String jsonstr = gsonobj.toJson(new_user);
jsonobj = new JSONObject(jsonstr);
} catch (JSONException ex) {
Logger.getLogger(Authneticate.class.getName()).log(Level.SEVERE, null, ex);
}
return jsonobj;
}
但我得到这样的错误,
type Exception report
message com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type text/html; charset=utf-8 was not found
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type text/html; charset=utf-8 was not found
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.codehaus.jettison.json.JSONObject, and Java type class org.codehaus.jettison.json.JSONObject, and MIME media type text/html; charset=utf-8 was not found
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561)
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
org.apache.jsp.HomeUser_jsp._jspService(HomeUser_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
如果我删除了查询语句,并取消注释代码,那么该块是
Session session = fact.getCurrentSession();
Transaction tx = session.beginTransaction();
//Query query = session.createQuery("from user where employeeid= :employee");
//query.setParameter("employee", employeeid);
new_user = (User) session.get(User.class, (long)277);
//new_user = (User) query.list();
然后它可以工作并给出预期的结果,empid=277
它是主键。
我对查询做错了什么。还有没有其他方法可以使用休眠来获取没有主键的数据,例如
`new_user = (User) session.get(User.class, employeeid);`
|
String(Not the primary key)
我的获取 JSON 对象的 jsp 文件是
<%
Client client = Client.create();
WebResource service = client.resource("http://localhost:8080/ITHelpdesk/webresources/hello/getjson");
String input = request.getParameter("username");
ClientResponse cliresponse = service.type("text/html").post(ClientResponse.class,input);
JSONObject jsonobj = cliresponse.getEntity(JSONObject.class);
%>
我试过使用new_user = (User) query.uniqueResult();
然后也发生了同样的错误 JSONObject jsonobj = cliresponse.getEntity(JSONObject.class);
谢谢