Skip to content

Commit f284423

Browse files
author
Frank
committed
updated rest template example
1 parent 49fd4fa commit f284423

2 files changed

Lines changed: 49 additions & 42 deletions

File tree

spring-resttemplate/src/main/java/com/concretepage/controller/PersonController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.concretepage.entity.Address;
44
import com.concretepage.entity.Company;
55
import com.concretepage.entity.Person;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.http.HttpHeaders;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.http.MediaType;
@@ -14,6 +15,7 @@
1415
import org.springframework.web.bind.annotation.RestController;
1516
import org.springframework.web.util.UriComponentsBuilder;
1617

18+
@Slf4j
1719
@RestController
1820
@RequestMapping("/data")
1921
public class PersonController {
@@ -84,6 +86,7 @@ public ResponseEntity<Void> locationURI(@PathVariable(value = "id") Integer id,
8486
System.out.println("Village:" + address.getVillage() + " District:" + address.getDistrict());
8587
HttpHeaders headers = new HttpHeaders();
8688
headers.setLocation(builder.path("/location/{id}/{name}").buildAndExpand(id, name).toUri());
89+
log.info("headers: {}", headers);
8790
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
8891
}
8992
}

spring-resttemplate/src/test/java/com/concretepage/controller/RestTemplateTest.java

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.concretepage.entity.Address;
44
import com.concretepage.entity.Company;
55
import com.concretepage.entity.Person;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.junit.Before;
78
import org.junit.Test;
89
import org.springframework.http.HttpEntity;
@@ -16,39 +17,35 @@
1617
import java.util.HashMap;
1718
import java.util.Map;
1819

20+
@Slf4j
1921
public class RestTemplateTest {
20-
RestTemplate restTemplate;
22+
private RestTemplate restTemplate;
2123

2224
@Before
2325
public void setUp() throws Exception {
2426
restTemplate = new RestTemplate();
2527
}
2628

2729
@Test
28-
public void testDelete() {
29-
String url = "http://localhost:8080/data/delete/{name}/{village}";
30-
Map<String, String> map = new HashMap<String, String>();
31-
map.put("name", "Mahesh");
32-
map.put("village", "Dhananjaypur");
33-
restTemplate.delete(url, map);
30+
public void testGetForObjectWithJson() {
31+
Person person = restTemplate.getForObject("http://localhost:8080/data/fetchjson/{id}", Person.class, 200);
32+
System.out.println("ID: " + person.getId());
33+
System.out.println("Name: " + person.getName());
34+
System.out.println("Village Name: " + person.getAddress().getVillage());
3435
}
3536

3637
@Test
37-
public void testExchange() {
38-
String uri = "http://localhost:8080/data/exchange/{id}";
39-
HttpHeaders headers = new HttpHeaders();
40-
headers.setContentType(MediaType.APPLICATION_JSON);
41-
HttpEntity<String> entity = new HttpEntity<String>("Hello World!", headers);
42-
ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
43-
System.out.println("ID:" + personEntity.getBody().getId());
44-
System.out.println("Name:" + personEntity.getBody().getName());
45-
System.out.println("Village:" + personEntity.getBody().getAddress().getVillage());
38+
public void testGetObjectWithXml() {
39+
Company company = restTemplate.getForObject("http://localhost:8080/data/fetchxml/{id}", Company.class, 200);
40+
System.out.println("ID: " + company.getId());
41+
System.out.println("Company: " + company.getCompanyName());
42+
System.out.println("CEO: " + company.getCeoName());
4643
}
4744

4845
@Test
4946
public void testGetForEntity() {
5047
String url = "http://localhost:8080/data/fetch/{name}/{village}";
51-
Map<String, String> map = new HashMap<String, String>();
48+
Map<String, String> map = new HashMap<>();
5249
map.put("name", "Mahesh");
5350
map.put("village", "Dhananjaypur");
5451
ResponseEntity<Person> personEntity = restTemplate.getForEntity(url, Person.class, map);
@@ -57,21 +54,17 @@ public void testGetForEntity() {
5754
}
5855

5956
@Test
60-
public void testGetForObjectWithJson() {
61-
Person person = restTemplate.getForObject("http://localhost:8080/data/fetchjson/{id}", Person.class, 200);
62-
System.out.println("ID: " + person.getId());
63-
System.out.println("Name: " + person.getName());
64-
System.out.println("Village Name: " + person.getAddress().getVillage());
65-
}
66-
67-
@Test
68-
public void testGetObjectWithXml() {
69-
70-
Company company = restTemplate.getForObject("http://localhost:8080/data/fetchxml/{id}", Company.class, 200);
71-
System.out.println("ID: " + company.getId());
72-
System.out.println("Company: " + company.getCompanyName());
73-
System.out.println("CEO: " + company.getCeoName());
57+
public void testExchange() {
58+
String uri = "http://localhost:8080/data/exchange/{id}";
59+
HttpHeaders headers = new HttpHeaders();
60+
headers.setContentType(MediaType.APPLICATION_JSON);
61+
HttpEntity<String> entity = new HttpEntity<>("Hello World!", headers);
62+
log.info("entity: {}", entity);
7463

64+
ResponseEntity<Person> personEntity = restTemplate.exchange(uri, HttpMethod.GET, entity, Person.class, 100);
65+
System.out.println("ID:" + personEntity.getBody().getId());
66+
System.out.println("Name:" + personEntity.getBody().getName());
67+
System.out.println("Village:" + personEntity.getBody().getAddress().getVillage());
7568
}
7669

7770
@Test
@@ -82,10 +75,23 @@ public void testHeadForHeaders() {
8275
System.out.println(httpHeaders.getContentType());
8376
}
8477

78+
79+
@Test
80+
public void testPostForObject() {
81+
String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
82+
Map<String, String> map = new HashMap<>();
83+
map.put("id", "111");
84+
map.put("name", "Shyam");
85+
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
86+
Person person = restTemplate.postForObject(url, address, Person.class, map);
87+
System.out.println(person.getName());
88+
System.out.println(person.getAddress().getVillage());
89+
}
90+
8591
@Test
8692
public void testPostForEntity() {
8793
String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
88-
Map<String, String> map = new HashMap<String, String>();
94+
Map<String, String> map = new HashMap<>();
8995
map.put("id", "111");
9096
map.put("name", "Shyam");
9197
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
@@ -99,19 +105,17 @@ public void testPostForLocation() {
99105
String url = "http://localhost:8080/data/location/{id}/{name}";
100106
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
101107
URI uri = restTemplate.postForLocation(url, address, 111, "Shyam");
108+
log.info("uri: {}", uri);
102109
System.out.println(uri.getPath());
103110
}
104111

105112
@Test
106-
public void testPostForObject() {
107-
String url = "http://localhost:8080/data/saveinfo/{id}/{name}";
108-
Map<String, String> map = new HashMap<String, String>();
109-
map.put("id", "111");
110-
map.put("name", "Shyam");
111-
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
112-
Person person = restTemplate.postForObject(url, address, Person.class, map);
113-
System.out.println(person.getName());
114-
System.out.println(person.getAddress().getVillage());
113+
public void testDelete() {
114+
String url = "http://localhost:8080/data/delete/{name}/{village}";
115+
Map<String, String> map = new HashMap<>();
116+
map.put("name", "Mahesh");
117+
map.put("village", "Dhananjaypur");
118+
restTemplate.delete(url, map);
115119
}
116120

117121
@Test
@@ -123,4 +127,4 @@ public void testPut() {
123127
Address address = new Address("Dhananjaypur", "Varanasi", "UP");
124128
restTemplate.put(url, address, map);
125129
}
126-
}
130+
}

0 commit comments

Comments
 (0)