1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
@SpringBootTest class TestControllerTest {
@Autowired RestTemplate restTemplate;
@Test void getForEntity() { ResponseEntity<User> response = restTemplate.getForEntity("http://localhost:8888/user", User.class); User body = response.getBody(); System.out.println(body); }
@Test void getForObject() { User user = restTemplate.getForObject("http://localhost:8888/user", User.class); System.out.println(user); }
@Test void exchange() {
HttpHeaders httpHeaders = new HttpHeaders(); HttpEntity httpEntity = new HttpEntity(null, httpHeaders); ResponseEntity<User> exchange = restTemplate.exchange("http://localhost:8888/user", HttpMethod.GET, httpEntity, User.class); User body = exchange.getBody(); System.out.println(body); }
@Test void getForEntityByParams() { Map<String, Object> params = new HashMap<>(); params.put("name", "当我遇上你"); params.put("age", 17); ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8888/user/1?name={name}&age={age}", String.class, params); System.out.println(responseEntity.getBody()); }
@Test void getForObjectByParams() { Map<String, Object> params = new HashMap<>(); params.put("id", 1); params.put("name", "当我遇上你"); params.put("age", 17); String res = restTemplate.getForObject("http://localhost:8888/user/{id}?name={name}&age={age}", String.class, params); System.out.println(res); }
@Test void getExchangeByParams() {
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity httpEntity = new HttpEntity(headers);
Map<String, Object> params = new HashMap<>(); params.put("id", 1); params.put("name", "当我遇上你"); params.put("age", 17);
ResponseEntity<String> exchange = restTemplate.exchange("http://localhost:8888/user/{id}?name={name}&age={age}", HttpMethod.GET, httpEntity, String.class, params); System.out.println(exchange.getBody()); }
@Test void getExchangeByUriComponentsBuilder() {
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity httpEntity = new HttpEntity(headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8888/user/1") .queryParam("name", "当我遇上你") .queryParam("age", 17);
ResponseEntity<String> exchange = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, httpEntity, String.class); System.out.println(exchange.getBody()); }
@Test void postForObject() {
HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", "777"); headers.setContentType(MediaType.APPLICATION_JSON); User body = new User("当我遇上你", "123"); HttpEntity<User> request = new HttpEntity<>(body, headers); User user = restTemplate.postForObject("http://localhost:8888/save", request, User.class); System.out.println(user); }
@Test void list1() { User[] users = restTemplate.getForObject("http://localhost:8888/list", User[].class); List<User> list = Arrays.asList(users); System.out.println(list); }
@Test void list2() { ResponseEntity<List<User>> responseEntity = restTemplate.exchange("http://localhost:8888/list", HttpMethod.GET, null, new ParameterizedTypeReference<List<User>>(){}); List<User> body = responseEntity.getBody(); System.out.println(body); } }
|