Java:从POST获取响应
我正在开发一个应用程序。我正在使用spring和RESTful Web服务。我向Web服务发送JSON,然后收到一个 我用firefox RESTClient插件测试了Web服务。但现在我正在开发客户机(将json发送到Web服务并接收响应的部分)。这是发送JSON的部分:Java:从POST获取响应,java,json,restful-architecture,Java,Json,Restful Architecture,我正在开发一个应用程序。我正在使用spring和RESTful Web服务。我向Web服务发送JSON,然后收到一个 我用firefox RESTClient插件测试了Web服务。但现在我正在开发客户机(将json发送到Web服务并接收响应的部分)。这是发送JSON的部分: String reqStr = "{\"customer\":\"test\",\"age\":\"24\"}"; String request = "example.com
String reqStr = "{\"customer\":\"test\",\"age\":\"24\"}";
String request = "example.com";
URL url = new URL(request);
HttpURLConnection rc = (HttpURLConnection)url.openConnection();
rc.setRequestMethod("POST");
rc.setDoOutput( true );
rc.setDoInput( true );
rc.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
rc.setRequestProperty("charset", "utf-8");
int len = reqStr.length();
rc.setRequestProperty( "Content-Length", Integer.toString( len ) );
rc.connect();
OutputStreamWriter out = new OutputStreamWriter( rc.getOutputStream() );
out.write( reqStr, 0, len );
out.flush();
我认为这不是通过POST发送JSON的最佳方式。但我真正的问题是
Charel您可以对使用
@Form
参数调用的函数进行注释。这将允许您读入这些值,类似于当您需要解析查询字符串时,@QueryParam
在GET
上的工作方式
在测试方面,您可能希望测试通过请求获得的JSON数据,这应该相当简单。为什么您必须使用内容类型作为application/x-www-form-urlencoded而不是application/JSON?