Question

I am using spring rest template to send json array as request. Source code to send request is as follow:

JSONArray jsonArray = new JSONArray();

for (Iterator iterator = itemlist.iterator(); iterator.hasNext();) {

    Item item = (Item)iterator.next();

    JSONObject formDetailsJson = new JSONObject();

    formDetailsJson.put("id", item.getItemConfId());
    formDetailsJson.put("name", item.getItems().getItemName());
    formDetailsJson.put("price", item.getPrice());
    formDetailsJson.put("Cost",item.getCost());

    jsonArray.put(formDetailsJson);
}


List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);

// Prepare header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
// Pass the new person and header
HttpEntity<JSONArray> entity = new HttpEntity<JSONArray>(jsonArray, headers);

System.out.println("Json Object : "+entity);
// Send the request as POST
try {
    ResponseEntity<String> result = restTemplate.exchange("my url", HttpMethod.POST, entity, String.class);

} catch (Exception e) {
    logger.error(e);

    return "Connection not avilable please try again";
}

And to accept request:

@RequestMapping(value = "/testStock", method = RequestMethod.POST,headers="Accept=application/xml, application/json")
    public @ResponseBody int testStock(@RequestBody List<ItemList>  jsonArray) {

        logger.debug("Received request to connect ms access : "+jsonArray.size());

        //int returnSizecount = stockList.getStocklst().size();

        return 1;
    }

The problem is that it giving me following error: Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONArray].Any suggestion is greatly acceptable.

Was it helpful?

Solution

There are no MessageConverter for JSONArray, so I suggest do the following.

HttpEntity<JSONArray> entity = new HttpEntity<JSONArray>(jsonArray, headers);

Convert Class JSONArray to String, and add that to HttpEntity, you know use toString

java.lang.String toString()

      Make a JSON text of this JSONArray.

HttpEntity entity = new HttpEntity(jsonArray.toString(), headers);

Or change to Jackson implementation Spring have support to that. XD

If you dont want to do the above, consider create your own implementation of messageConverter, that will work but is harder

update

HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
headers.setContentType(MediaType.APPLICATION_JSON);

update 2 Change endpoint to.

@RequestMapping(value = "/testStock", method = RequestMethod.POST)
    public @ResponseBody int testStock(@RequestBody String  jsonArray) {

OTHER TIPS

you need to have httpmessageconverter configured for your resttemplate, please read my post for configuring http message conveter for you webservice

http://stackoverflow.com/questions/19963127/new-to-spring-and-jackson-2-what-does-this-bean-declaration-allow-for-in-a-spri/19973636#19973636.

and for you problem to convert your http request to json you might add this entry in your restemplate configuration

 <bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>

The error is quite straightforward. You do not have a converter for the JSONArray. Converting the array to a String (using toString) did help you here, but there is a better way:

Just add a converter for the json.org objects:

Add this to your pom.xml

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-json-org</artifactId>
    </dependency>

And then on your ObjectMapper add the JsonOrgModule:

mapper.registerModule(new JsonOrgModule());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top