Reputation: 181
Here is an snippet of the code that I use in my RESTful webservice. I am sending a JSON data to the create(Employee entity) method. I want to get that JSON data from the servlet request.
The System.out.println(..) method that I used gets printed which means stringBuffer is not NULL; yet nothing gets printed after ":" character [I am checking this in the glassfish logger].Besides, the POSTed enitity is being persisted to the database correctly.
I have googled many of the questions related to this on stackoverflow and other sites but nothing that I have tried works so far.
public class EmployeeFacadeREST extends AbstractFacade<Employee> {
@Context private HttpServletRequest servletRequest;
@PersistenceContext(unitName = "EmployeePU")
private EntityManager em;
public EmployeeFacadeREST() {
super(Employee.class);
}
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create( Employee entity) {
StringBuffer stringBuffer = null;
try{
InputStream body = servletRequest.getInputStream();
stringBuffer = new StringBuffer();
int d;
while((d = body.read()) != -1){
stringBuffer.append((char)d);
}
}
catch(IOException e){
}
if(stringBuffer != null){
System.out.println("the entity is: " + stringBuffer.toString()); // this line gets printed
}
super.create(entity);
}
// more code
}
Your insight is appreciated.
Upvotes: 0
Views: 109
Reputation: 47954
The server already consumed the request body in order to de-serialize it into the Employee entity for you automatically. You can't read the same input stream again.
Upvotes: 2
Reputation: 6289
You need to set the 'entity' used by the POST with the value of your stringbuffer.
check the tests or samples of a jersey /glassfish POST .
The POST.entity expects the value u want in the body of the POST.
Upvotes: 0