0

I'm developing an application. I'm using spring and RESTful webservices. I send JSON to the webservice and I receive one.

I tested the webservice with the firefox RESTClient plugin. But now I am developing the client (part that sends the json to the webservice and receives the response). This is the part to send the 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();

I have the idea that this is not the best way to send JSON via POST. But my real question is.

  1. How can I receive the POST response in my Java code?
  2. Is there a way to test this?

Kind regards, Charel

Charel
  • 1
  • Why do you have to use Content-Type as application/x-www-form-urlencoded instead of application/json? – Srinivas Jan 09 '13 at 17:06

1 Answers1

0

You can get away with annotating the function that is being invoked with the @Form parameter. This will allow you to read in those values, similar to how the @QueryParam works on GET when you need to parse out the query string.

In terms of testing, you would want to test the JSON data that comes through the request, that should be fairly trivial.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151