Pun*_*cky 17 java spring resttemplate
是否可以将标头设置为getForEntity方法的一部分,还是应该使用exchange?我试图将oauth标头设置为getForEntity调用的一部分.
sgr*_*lon 21
你可以使用.exchange
:
ResponseEntity<YourResponseObj> entity = new TestRestTemplate().exchange(
"http://localhost:" + port + "/youruri", HttpMethod.GET, new HttpEntity<Object>(headers),
YourResponseObj.class);
Run Code Online (Sandbox Code Playgroud)
完整的Junit样本:
@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());
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14524 次 |
最近记录: |