我想在我使用Spring的RestTemplate
的請求中設置Accept:
的值。如何在Spring RestTemplate請求上設置「Accept:」標題?
這裏是我的春節,請求處理代碼
@RequestMapping(
value= "/uom_matrix_save_or_edit",
method = RequestMethod.POST,
produces="application/json"
)
public @ResponseBody ModelMap uomMatrixSaveOrEdit(
ModelMap model,
@RequestParam("parentId") String parentId
){
model.addAttribute("attributeValues",parentId);
return model;
}
,這裏是我的Java REST客戶端:
public void post(){
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("parentId", "parentId");
String result = rest.postForObject(url, params, String.class) ;
System.out.println(result);
}
這對我的作品;我從服務器端得到一個JSON字符串。
我的問題是:我怎麼可以指定Accept:
頭(例如application/json
,application/xml
,...),並請求方法(例如GET
,POST
,...)當我使用RestTemplate?
如果您未設置接受標頭,默認情況下在restTemplate.exchange()中設置MediaType.APPLICATION_JSON。 –