0

I have a springboot project that takes only POST JSON String which I'm converting to HashMap using gson. I tested using Postman as a POST and add the body as props with a json string like {'fistname': 'John', 'lastname' : 'Doe'}, translates to props = {'fistname': 'John', 'lastname' : 'Doe'}. its working as expected

@RequestMapping(value = "/rest", method = RequestMethod.POST)
    protected String parse(@RequestParam("props") String props) {
    Gson gson = new Gson();
    Map<String, String> params = new HashMap<String, String>();
    params = gson.fromJson(props, Map.class);

    // Rest of the process
}

On the other hand, I have a JavaEE project, which needs to call this API

protected void callREST() {

      try {
            String json = someClass.getDate() //retrieved from database which is stored as json structure
            Map<String, String> props = gson.fromJson(json, Map.class);

            URL url = new URL("http://localhost:9090/myApp/rest");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");

            DataOutputStream wr = new DataOutputStream( conn.getOutputStream());

            System.out.println(props.toString());
            wr.writeBytes(json.toString());
            wr.flush();
            wr.close();
            if(conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
                throw new RuntimeException("Failed :: HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String output;
            System.out.println("Output from Server ... \n");
            while((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

       } catch(Exception e) {
          //print stack trace
       }
 }

I get Failed :: HTTP error code : 400. I suspect the spring boot is not receiving the data in props variable since its a POST request. What should i add in the client code to pass the props and the data to make this call successful ?

Note: JavaEE is running on tomcat :8080, Springboot is running on different tomcat :9090

1
  • 1
    Have a look at Spring"s RestTemplate class, it is very useful for this. Commented Aug 16, 2017 at 15:56

2 Answers 2

1

@RequestParam means that server awaits param in request URL http://localhost:9090/myApp/rest?param=..... but in you client you are writing JSON in body of request.

Try to use @RequestBody annotation in your endpoint

protected String parse(@RequestBody String props) {...}
Sign up to request clarification or add additional context in comments.

1 Comment

How can I make this async ?
1

Your resources expects to get form parameters (i.e. key value pairs, using x-www-form-urlencoded encoding), where the value happens to be JSON (although what you posted is not valid JSON).

But your client Java code sets the content type to application/json, and sends the JSON as the body, instead of sending it as the value of the key "props" of a x-www-form-urlencoded body.

So that can't work.

If you can change the server, then do that. Accept JSON as the body directly:

@RequestMapping(value = "/rest", method = RequestMethod.POST)
public String parse(@RequestBody Map<String, String> map) {
     ...
}

If not, you'll need to send the correct key value pair, and make sure the value is properly url-encoded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.