diff --git a/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/RestTemplateExceptionApplication.java b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/RestTemplateExceptionApplication.java new file mode 100644 index 0000000000..84a337f5ee --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/RestTemplateExceptionApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.resttemplateexception; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class RestTemplateExceptionApplication { + + public static void main(String[] args) { + SpringApplication.run(RestTemplateExceptionApplication.class, args); + } + +} diff --git a/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/controller/ProductApi.java b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/controller/ProductApi.java new file mode 100644 index 0000000000..2c530cae2b --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/controller/ProductApi.java @@ -0,0 +1,46 @@ +package com.baeldung.resttemplateexception.controller; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.resttemplateexception.model.Criterion; +import com.baeldung.resttemplateexception.model.Product; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RestController +@RequestMapping("/api") +public class ProductApi { + + private List productList = new ArrayList<>(Arrays.asList(new Product(1, "Acer Aspire 5", 437), new Product(2, "ASUS VivoBook", 650), new Product(3, "Lenovo Legion", 990))); + + @GetMapping("/get") + public Product get(@RequestParam String criterion) throws JsonMappingException, JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + Criterion crt = objectMapper.readValue(criterion, Criterion.class); + if (crt.getProp().equals("name")) + return findByName(crt.getValue()); + + // Search by other properties (id,price) + + return null; + } + + private Product findByName(String name) { + for (Product product : this.productList) { + if (product.getName().equals(name)) { + return product; + } + } + return null; + } + + // Other methods +} diff --git a/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/model/Criterion.java b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/model/Criterion.java new file mode 100644 index 0000000000..9a96ad4dc3 --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/model/Criterion.java @@ -0,0 +1,28 @@ +package com.baeldung.resttemplateexception.model; + +public class Criterion { + + private String prop; + private String value; + + public Criterion() { + + } + + public String getProp() { + return prop; + } + + public void setProp(String prop) { + this.prop = prop; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + +} diff --git a/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/model/Product.java b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/model/Product.java new file mode 100644 index 0000000000..e4cc29279c --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/main/java/com/baeldung/resttemplateexception/model/Product.java @@ -0,0 +1,43 @@ +package com.baeldung.resttemplateexception.model; + +public class Product { + + private int id; + private String name; + private double price; + + public Product() { + + } + + public Product(int id, String name, double price) { + this.id = id; + this.name = name; + this.price = price; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + +} diff --git a/spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplateexception/RestTemplateExceptionLiveTest.java b/spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplateexception/RestTemplateExceptionLiveTest.java new file mode 100644 index 0000000000..adfb8ffa4e --- /dev/null +++ b/spring-web-modules/spring-resttemplate-2/src/test/java/com/baeldung/resttemplateexception/RestTemplateExceptionLiveTest.java @@ -0,0 +1,47 @@ +package com.baeldung.resttemplateexception; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import com.baeldung.resttemplateexception.model.Product; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { RestTemplate.class, RestTemplateExceptionApplication.class }) +public class RestTemplateExceptionLiveTest { + + @Autowired + RestTemplate restTemplate; + + @Test(expected = IllegalArgumentException.class) + public void givenGetUrl_whenJsonIsPassed_thenThrowException() { + String url = "http://localhost:8080/spring-rest/api/get?criterion={\"prop\":\"name\",\"value\":\"ASUS VivoBook\"}"; + Product product = restTemplate.getForObject(url, Product.class); + } + + @Test + public void givenGetUrl_whenJsonIsPassed_thenGetProduct() { + String criterion = "{\"prop\":\"name\",\"value\":\"ASUS VivoBook\"}"; + String url = "http://localhost:8080/spring-rest/api/get?criterion={criterion}"; + Product product = restTemplate.getForObject(url, Product.class, criterion); + + assertEquals(product.getPrice(), 650, 0); + } + + @Test + public void givenGetUrl_whenJsonIsPassed_thenReturnProduct() { + String criterion = "{\"prop\":\"name\",\"value\":\"Acer Aspire 5\"}"; + String url = "http://localhost:8080/spring-rest/api/get"; + + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url).queryParam("criterion", criterion); + Product product = restTemplate.getForObject(builder.build().toUri(), Product.class); + + assertEquals(product.getId(), 1, 0); + } +}