2013-10-08 53 views
112

我想在我使用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/jsonapplication/xml,...),並請求方法(例如GETPOST,...)當我使用RestTemplate?

回答

215

我建議使用一個接受HttpEntity,而您也可以設置HttpHeadersexchange方法之一。 (您也可以指定要使用的HTTP方法。)

例如,

RestTemplate restTemplate = new RestTemplate(); 
HttpHeaders headers = new HttpHeaders(); 
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); 

HttpEntity<String> entity = new HttpEntity<>("parameters", headers); 

restTemplate.exchange(url, HttpMethod.POST, entity, String.class); 

我喜歡這個解決方案,因爲它是強類型的,即。 exchange預計HttpEntity

但是,您也可以將HttpEntity作爲request參數傳遞給postForObject

HttpEntity<String> entity = new HttpEntity<>("parameters", headers); 
restTemplate.postForObject(url, entity, String.class); 

這是在RestTemplate#postForObject Javadoc中提到的。

request參數可以是爲了一個HttpEntity添加額外 HTTP標頭的請求

+0

如果您未設置接受標頭,默認情況下在restTemplate.exchange()中設置MediaType.APPLICATION_JSON。 –

75

您可以在RestTemplate中設置攔截器「ClientHttpRequestInterceptor」,以避免每次發送請求時都設置標題。

public class HeaderRequestInterceptor implements ClientHttpRequestInterceptor { 

     private final String headerName; 

     private final String headerValue; 

     public HeaderRequestInterceptor(String headerName, String headerValue) { 
      this.headerName = headerName; 
      this.headerValue = headerValue; 
     } 

     @Override 
     public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { 
      request.getHeaders().set(headerName, headerValue); 
      return execution.execute(request, body); 
     } 
    } 

然後

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); 
interceptors.add(new HeaderRequestInterceptor("Accept", MediaType.APPLICATION_JSON_VALUE)); 

RestTemplate restTemplate = new RestTemplate(); 
restTemplate.setInterceptors(interceptors); 
+5

爲什麼你需要HttpRequestWrapper在這裏? – nahab

+2

我接受你的方法更好,因爲它允許在真正的春天使用Spring,使用特殊方法'postForObject'等,而不是'exchange'。謝謝! – Daria

+0

Spring Boot 1.3有一個HttpHeaderInterceptor,所以我們不需要創建我們自己的ClientHttpRequestInterceptor實現。 –

7

如果像我一樣,你很難找到一個使用基本身份驗證,其餘模板交換API頭一個例子,這是我終於摸索出...

private HttpHeaders createHttpHeaders(String user, String password) 
{ 
    String notEncoded = user + ":" + password; 
    String encodedAuth = Base64.getEncoder().encodeToString(notEncoded.getBytes()); 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.APPLICATION_JSON); 
    headers.add("Authorization", "Basic " + encodedAuth); 
    return headers; 
} 

private void doYourThing() 
{ 
    String theUrl = "http://blah.blah.com:8080/rest/api/blah"; 
    RestTemplate restTemplate = new RestTemplate(); 
    try { 
     HttpHeaders headers = createHttpHeaders("fred","1234"); 
     HttpEntity<String> entity = new HttpEntity<String>("parameters", headers); 
     ResponseEntity<String> response = restTemplate.exchange(theUrl, HttpMethod.GET, entity, String.class); 
     System.out.println("Result - status ("+ response.getStatusCode() + ") has body: " + response.hasBody()); 
    } 
    catch (Exception eek) { 
     System.out.println("** Exception: "+ eek.getMessage()); 
    } 
} 
相關問題