Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从POST获取响应_Java_Json_Restful Architecture - Fatal编程技术网

Java:从POST获取响应

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

我正在开发一个应用程序。我正在使用spring和RESTful Web服务。我向Web服务发送JSON,然后收到一个

我用firefox RESTClient插件测试了Web服务。但现在我正在开发客户机(将json发送到Web服务并接收响应的部分)。这是发送JSON的部分:

            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的最佳方式。但我真正的问题是

  • 如何在Java代码中接收POST响应
  • 有没有办法测试这个
  • 亲切问候,,
    Charel

    您可以对使用
    @Form
    参数调用的函数进行注释。这将允许您读入这些值,类似于当您需要解析查询字符串时,
    @QueryParam
    GET
    上的工作方式


    在测试方面,您可能希望测试通过请求获得的JSON数据,这应该相当简单。

    为什么您必须使用内容类型作为application/x-www-form-urlencoded而不是application/JSON?