2014-02-21 56 views
2

我使用spring rest模板發送json數組作爲請求。源代碼發送請求如下:Spring Rest模板發送JsonArray

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"; 
} 

,並接受請求:

@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; 
    } 

的問題是,它給我以下錯誤: 無法寫入請求:發現請求沒有合適HttpMessageConverter鍵入[org.json.JSONArray]。任何建議都是非常可接受的。

回答

3

JSONArray沒有MessageConverter,所以我建議做以下操作。

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

轉換類JSONArray爲字符串,並添加到HttpEntity,你知道使用的toString

java.lang.String toString()

 Make a JSON text of this JSONArray. 

HttpEntity實體=新HttpEntity(jsonArray.toString(),頭);

或者更改爲Jackson實現Spring支持這一點。 XD

,如果你不想做上述情況,考慮創建自己的實現的MessageConverter的,將工作,但更難

更新

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

更新2更改端點。

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

將其轉換爲的ToString()。對於請求接受,它在服務器端給我415不支持的媒體類型。 – User

+0

添加一個這樣的頭文件:header.setContentType(MediaType.APPLICATION_JSON),這個telll服務器的字符串是一個json,請讓我知道如果這樣的作品 – Koitoer

+0

我已經將它作爲媒體類型添加到我的代碼中。在我的代碼中,在服務器端做什麼意味着testStock方法。 – User

1

你需要有httpmessageconverter配置爲您的resttemplate,請閱讀我的文章用於配置HTTP消息conveter你的WebService

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

,併爲您的問題轉換您的HTTP請求到JSON您可以添加這個入口在你的模板配置中

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

我認爲jsonarray無法轉換爲該MessageConverter,因爲JsonArray不屬於傑克遜庫 – Koitoer

0

錯誤很簡單。您沒有用於JSONArray的轉換器。數組轉換成字符串(使用的toString)並幫助你在這裏,但有一個更好的辦法:

只需添加一個轉換器的json.org對象:

添加到您的pom.xml

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

,然後在你的ObjectMapper添加JsonOrgModule:

mapper.registerModule(new JsonOrgModule());