0
我有一個Jersey Web服務,我需要解析與請求一起發送的一些json數據。已爲此請求調用java.lang.IllegalStateException:getInputStream()
@POST
@Path ("/authenticate")
@Produces (MediaType.APPLICATION_JSON)
public Response authenticate (@Context HttpServletRequest request)
{
try {
StringBuffer json = new StringBuffer();
BufferedReader reader = request.getReader();
int line;
while ((line = reader.readLine()) != null)
{
json.append(line);
}
System.out.prinln (json);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return Response.ok().entity(json).build();
}//end authenticate method
該服務產生以下異常:
java.lang.IllegalStateException: getInputStream() has already been called for this request
我做了一些研究。這表明一個getReader
和getInputStream
不能在同一請求調用。因此,它似乎已經調用了一個getInputStream
實例。如果我沒有明確地打電話,這怎麼可能?爲了解決這個問題,我使用了getInputStream
方法,而不是
try {
ServletInputStream reader = request.getInputStream();
int line;
while ((line = reader.read()) != -1)
{
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return Response.ok().entity().build();
用這種方法,我該如何使用字節的INT獲得JSON?
爲什麼你不讓球衣生成JSON你的目標? –