'JSP and response entities
I am calling an endpoint from jsp:
@GET
@Path("/select-users")
@Produces(MediaType.TEXT_PLAIN)
public ResultSet selectUsers() throws SQLException {
PreparedStatement statement = appService.connect().prepareStatement("SELECT * FROM users");
ResultSet result = statement.executeQuery();
return result;
}
which returns object type ResultSet.
From .jsp I want to save the response in that object in order to parse the results of the query.
<%
Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/StudentsCRM/rest/controller/select-users");
ClientResponse myresponse = webResource.accept("text/plain").get(ClientResponse.class);
ResultSet rs = myresponse.getEntity(java.sql.ResultSet.class);
%>
ERROR: com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.sql.ResultSet, and Java type interface java.sql.ResultSet, and MIME media type application/octet-stream was not found
Question: Is this the right way to save the ResultSet response?
Solution 1:[1]
The problem is in your code:
@Produces(MediaType.TEXT_PLAIN)
public ResultSet selectUsers() throws SQLException {
PreparedStatement statement = appService.connect().prepareStatement("SELECT * FROM users");
ResultSet result = statement.executeQuery();
return result;
}
According to javadoc
Produces annotation defines the media type(s) that the methods of a resource class or MessageBodyWriter can produce.
You declare, that it produces plain text. But your method does NOT return plain text. It returns ResultSet. I think you don't need plain text. You need some data format that can keep users, for example, JSON or XML.
If you choose JSON, then you should iterate your result set to get a list of users, and return it. Besides, you will need to set correct Produces type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pavel_K |
