2017-05-10 29 views
1

getForEntityメソッドの一部としてヘッダーを設定することはできますか?交換を使用する必要がありますか?私はgetForEntity呼び出しの一部としてoauthヘッダーを設定しようとしています。Resttemplate getForEntity - ヘッダーを渡す

+3

ResponseEntity<YourResponseObj> entity = new TestRestTemplate().exchange( "http://localhost:" + port + "/youruri", HttpMethod.GET, new HttpEntity<Object>(headers), YourResponseObj.class); 

全JUnitのサンプルを。 docs https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.htmlを見ると、getForEntityはObjectまたはHttpEntityを引数として取らない – pvpkiran

+0

ありがとう@pvpkiran –

答えて

1

あなたは.exchange使用することができます:あなたが交流を使用する必要が

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 
public class ReferenceTablesControllerTests { 

    @LocalServerPort 
    private int port; 

    @Test 
    public void getXxxx() throws Exception { 
     MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); 
     headers.add("Content-Type", "application/json"); 
     headers.add("Authorization", "tokenxxx"); 
     ResponseEntity<YourResponseObj> entity = new TestRestTemplate().exchange(
       "http://localhost:" + port + "/youruri", HttpMethod.GET, new HttpEntity<Object>(headers), 
       YourResponseObj.class); 
     Assert.assertEquals(HttpStatus.OK, entity.getStatusCode()); 
     Assert.assertEquals("foo", entity.getBody().getFoo()); 
    } 

} 
関連する問題