如何在servlet中读取来自客户端的application / json数据(java)

时间:2012-12-29 13:04:20

标签: json servlets

这是我要去服务器的请求

POST /com.restserver/rest/auth HTTP/1.1  
Accept-Encoding: gzip,deflate  
Content-Type: application/json  
User-Agent: Jakarta Commons-HttpClient/3.1  
Host: localhost:9991  
Proxy-Connection: Keep-Alive  
Content-Length: 28

{"uname":"abc","password":"xyz"}

////请求结束

我正在向我的servlet发送请求。在请求帖子正文中我有应用程序/ json数据。我无法使用request.getParameter获取json数据。我还尝试使用request.getInputStream()来获取这个json数据。任何人都可以告诉我如何访问来自servlet中的客户端的json数据。在我的请求中,我以json格式从客户端向服务器发送uname和密码。我想获取uname和密码值。

这是我的servlet类:

import javax.ws.rs.Consumes;  

import javax.ws.rs.POST;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.Context;  
import javax.ws.rs.core.MediaType;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

@Path("/auth")  
public class abc {  
        @POST  
        @Produces(MediaType.APPLICATION_JSON)  
        @Consumes(MediaType.APPLICATION_JSON)  
        public Model newTodo(  
          @Context HttpServletResponse response,@Context HttpServletRequest request)     throws   IOException,SQLException, ParseException   




 ///here I want to get  uname and password value that is coming from the         request into some string.

         //  I am able to get the content length of the request that is 28 however i am not able to fetch the content(uname and password values).  

            }  
       }  

0 个答案:

没有答案