РАВИ
РАВИ

Reputation: 12215

send json data through a post call by using rest template from spring

I keep getting an error saying that this is not valid json data and keep getting error 400. I don't think the implementation I have is formatting data properly. Goal: trying to make a post call using a rest template, by passing in JSON data as the body. It seems that when converting from map to json data, it isn't properly converted to json.

public void GetJsonData(String name, String city) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("api_key", apikey);



map.put("venue_queries", "[{'name':'"+name+"', 'location': {'locality': '"+city+"'}}]");

        try {
            String resp = GlobalHelper.calltestService(RestServicesUrl, map);
            System.out.println(resp);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


public String calltestService(String url, Map<String, String> data) throws Exception {
        RestTemplate rest = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        ResponseEntity<String> response = new ResponseEntity<String>(headers, HttpStatus.OK);
        rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            Gson gson = new Gson();
            System.out.println(gson.toJson(data));
        try {
            response = rest.postForEntity(url, gson.toJson(data), String.class);
            System.out.println(response.getBody()); 
            System.out.println(response.getHeaders());
            return response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error in post entity");
            return "error";
        }
    }

Upvotes: 0

Views: 11085

Answers (1)

vzamanillo
vzamanillo

Reputation: 10544

Right, your resulting JSON is not valid

{
    "venue_queries": "[{\u0027name\u0027:\u0027yo\u0027, \u0027location\u0027: {\u0027locality\u0027: \u0027Solares\u0027}}]",
    "api_key": "asfasdfasfdasdfasdfa"
}

you JSON is not valid because GSON escapes the HTML characters by default and put the single quotes as the equivalent unicode char code U+0027

if you disable the Gson HTMLEscaping

GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
Gson gson = builder.create();

you will get

{
    "venue_queries": "[{'name':'yo', 'location': {'locality': 'Solares'}}]",
    "api_key": "asfasdfasfdasdfasdfa"
}

anyway, your JSON is still incorrect.

You have to fix your venue_queries key content int the map using double quotes "

map.put("venue_queries", "[{\"name\":\""+name+"\", \"location\": {\"locality\": \""+city+"\"}}]");

then your resulting JSON will be valid

{
    "venue_queries": "[{\"name\":\"yo\", \"location\": {\"locality\": \"Solares\"}}]",
    "api_key": "asfasdfasfdasdfasdfa"
}

Tes snippet

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gson.Gson;

public class GsonMap {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        String apikey = "asfasdfasfdasdfasdfa";
        map.put("api_key", apikey);
        String name = "yo";
        String city = "Solares";
        map.put("venue_queries", "[{\"name\":\""+name+"\", \"location\": {\"locality\": \""+city+"\"}}]");
        Gson gson = new Gson();
        String json = gson.toJson(map);
        System.out.println("Resulting JSON:" + json);
        map = gson.fromJson(json, map.getClass());
        System.out.println("Resulting map values:");
        for (Entry<String, String> entry : map.entrySet()) {
            System.out.println("Key:" + entry.getKey() + ", value:" + entry.getValue());
        }
    }
}

Upvotes: 2

Related Questions