From e8df7f8116ad20a632c19ae97ee5c5ac2be5af96 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 29 Jul 2017 10:06:27 +0200 Subject: [PATCH] Taxi fare refactor (#2328) --- spring-rest/difference-uri-url-rest/pom.xml | 32 ------------------ .../baeldung/springboot/rest/Greeting.java | 21 ------------ .../springboot/rest/GreetingController.java | 21 ------------ .../rest/SpringBootWebApplication.java | 27 --------------- .../rest/client/ApplicationClient.java | 27 --------------- .../springboot/rest/client/Greeting.java | 33 ------------------- .../rest/test/DifferenceURIURLRESTTest.java | 32 ------------------ .../controller/DataProducerController.java | 2 +- .../com/baeldung/web/log/app/Application.java | 1 - .../log/app/TaxiFareRequestInterceptor.java | 4 +-- .../web/log/config/TaxiFareMVCConfig.java | 5 ++- .../log/controller/TaxiFareController.java | 9 +++-- .../com/baeldung/web/log/data/RateCard.java | 19 ++++++----- .../com/baeldung/web/log/data/TaxiRide.java | 11 ++++--- .../service/TaxiFareCalculatorService.java | 14 +++----- .../org/baeldung/config/MainApplication.java | 1 - .../repository/HeavyResourceRepository.java | 7 ++-- 17 files changed, 34 insertions(+), 232 deletions(-) delete mode 100644 spring-rest/difference-uri-url-rest/pom.xml delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java delete mode 100644 spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java delete mode 100644 spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java diff --git a/spring-rest/difference-uri-url-rest/pom.xml b/spring-rest/difference-uri-url-rest/pom.xml deleted file mode 100644 index 5ab6e63240..0000000000 --- a/spring-rest/difference-uri-url-rest/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - org.baeldung.springboot.rest - difference-uri-url-rest - 0.0.1-SNAPSHOT - war - - - 1.8 - - - - org.springframework.boot - spring-boot-starter-parent - 1.5.2.RELEASE - - - - - - org.springframework.boot - spring-boot-starter-web - compile - - - org.springframework.boot - spring-boot-starter-test - test - - - - \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java deleted file mode 100644 index cc166587df..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/Greeting.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springboot.rest; - -public class Greeting { - private static final long serialVersionUID = 1L; - - private Integer id = null; - private String content = null; - - public Greeting(Integer id) { - this.id = id; - this.content = "Hello World"; - } - - public Integer getId() { - return id; - } - - public String getContent() { - return content; - } -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java deleted file mode 100644 index 3fca9a1a76..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/GreetingController.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.springboot.rest; - -import java.util.concurrent.atomic.AtomicLong; - -import org.springframework.stereotype.Component; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -@RestController("/") -@Component -public class GreetingController { - - private final AtomicLong counter = new AtomicLong(); - - @RequestMapping(value = "/greetings", method = RequestMethod.GET) - public Greeting greeting() { - - return new Greeting(new Integer((int) counter.incrementAndGet())); - } -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java deleted file mode 100644 index 08d4c2c65e..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/SpringBootWebApplication.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.springboot.rest; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.support.SpringBootServletInitializer; - -@EnableAutoConfiguration -@SpringBootApplication -public class SpringBootWebApplication extends SpringBootServletInitializer { - - // method for explicit deployment on Application Server - @Override - protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { - return application.sources(SpringBootWebApplication.class); - } - - // run it as standalone JAVA application - public static void main(String[] args) throws Exception { - SpringApplication.run(SpringBootWebApplication.class, args); - } - - //Samples - // http://localhost:8080/greetings - // http://localhost:8989/difference-uri-url-rest/greetings -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java deleted file mode 100644 index 2d92c890c5..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/ApplicationClient.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.springboot.rest.client; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.client.RestTemplate; - -public class ApplicationClient { - //private static final Logger log = LoggerFactory.getLogger(ApplicationClient.class); - final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings"; - - - public ApplicationClient() { - super(); - } - - public Greeting init() { - RestTemplate restTemplate = new RestTemplate(); - Greeting greeting = restTemplate.getForObject(ApplicationClient.URI_STRING, Greeting.class); - //log.info(greeting.toString()); - return greeting; - } - public static void main(String args[]) { - Greeting greeting = new ApplicationClient().init(); - System.out.println(greeting.toString()); - } - -} diff --git a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java b/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java deleted file mode 100644 index 7d1119d155..0000000000 --- a/spring-rest/difference-uri-url-rest/src/main/java/com/baeldung/springboot/rest/client/Greeting.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.springboot.rest.client; - -import java.io.Serializable; - -public class Greeting implements Serializable { - private static final long serialVersionUID = 1L; - - private Integer id = null; - private String content = null; - - /** Default constructor is mandatory for client */ - public Greeting() { - super(); - } - - public Greeting(Integer id) { - this.id = id; - this.content = "Hello World"; - } - - public Integer getId() { - return id; - } - - public String getContent() { - return content; - } - - @Override - public String toString() { - return "Id: " + getId().toString() + " Content: " + getContent(); - } -} \ No newline at end of file diff --git a/spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java b/spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java deleted file mode 100644 index 4397f8e088..0000000000 --- a/spring-rest/difference-uri-url-rest/src/test/java/com/baeldung/springboot/rest/test/DifferenceURIURLRESTTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.springboot.rest.test; - -import static org.junit.Assert.assertNotNull; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.springframework.web.client.RestTemplate; - -import com.baeldung.springboot.rest.client.Greeting; - -public class DifferenceURIURLRESTTest { - final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings"; - static RestTemplate restTemplate; - Greeting greeting; - - @BeforeClass - public static void setupTest() { - restTemplate = new RestTemplate(); - } - - @Test - public void givenRestTenplate_whenIsNotNull_thenSuccess() { - assertNotNull("Rest Template not null", restTemplate); - } - - @Test - public void givenWiredConstructorParam_whenIsNotNull_thenSuccess() { - greeting = restTemplate.getForObject(URI_STRING, Greeting.class); - assertNotNull("Greeting class is not null", greeting); - } - -} diff --git a/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java b/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java index 6f34bdb9ae..ab233a8b60 100644 --- a/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java +++ b/spring-rest/src/main/java/com/baeldung/produceimage/controller/DataProducerController.java @@ -17,7 +17,7 @@ public class DataProducerController { return "Hello world"; } - @GetMapping(value = "/get-image") + @GetMapping("/get-image") public @ResponseBody byte[] getImage() throws IOException { final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg"); return IOUtils.toByteArray(in); diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java index 9bdbbd0d9f..41042008ef 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/Application.java @@ -14,5 +14,4 @@ public class Application extends SpringBootServletInitializer { public static void main(final String[] args) { SpringApplication.run(Application.class, args); } - } \ No newline at end of file diff --git a/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java b/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java index 831d236edd..b154f3665f 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java @@ -14,11 +14,11 @@ import com.baeldung.web.log.util.RequestLoggingUtil; @Component public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { - Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); + private final static Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String postData = null; + String postData; HttpServletRequest requestCacheWrapperObject = null; try { // Uncomment to produce the stream closed issue diff --git a/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java b/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java index fa26706ff0..f433b4f3c7 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java @@ -1,18 +1,17 @@ package com.baeldung.web.log.config; +import com.baeldung.web.log.app.TaxiFareRequestInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import com.baeldung.web.log.app.TaxiFareRequestInterceptor; - @Configuration public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter { @Autowired private TaxiFareRequestInterceptor taxiFareRequestInterceptor; - + @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/"); diff --git a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java index c39be88059..7de88d44a8 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java @@ -14,8 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody; import com.baeldung.web.log.data.RateCard; import com.baeldung.web.log.data.TaxiRide; import com.baeldung.web.log.service.TaxiFareCalculatorService; +import org.springframework.web.bind.annotation.RestController; -@Controller +@RestController public class TaxiFareController { @Autowired @@ -23,15 +24,13 @@ public class TaxiFareController { private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class); - @GetMapping(value = "/taxifare/get/") - @ResponseBody + @GetMapping("/taxifare/get/") public RateCard getTaxiFare() { LOGGER.debug("getTaxiFare() - START"); return new RateCard(); } - @PostMapping(value = "/taxifare/calculate/") - @ResponseBody + @PostMapping("/taxifare/calculate/") public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { LOGGER.debug("calculateTaxiFare() - START"); String totalFare = taxiFareCalculatorService.calculateFare(taxiRide); diff --git a/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java b/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java index 35ae38fd11..c7955b561b 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/data/RateCard.java @@ -4,25 +4,28 @@ public class RateCard { private String nightSurcharge; private String ratePerMile; - - public RateCard(){ - nightSurcharge="Extra $ 100"; - ratePerMile="$ 10 Per Mile"; + + public RateCard() { + nightSurcharge = "Extra $ 100"; + ratePerMile = "$ 10 Per Mile"; } - - + + public String getNightSurcharge() { return nightSurcharge; } + public void setNightSurcharge(String nightSurcharge) { this.nightSurcharge = nightSurcharge; } + public String getRatePerMile() { return ratePerMile; } + public void setRatePerMile(String ratePerMile) { this.ratePerMile = ratePerMile; } - - + + } diff --git a/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java b/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java index 0877cdced4..2e0f33f02b 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/data/TaxiRide.java @@ -5,14 +5,15 @@ public class TaxiRide { private Boolean isNightSurcharge; private Long distanceInMile; - public TaxiRide(){} - - public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){ + public TaxiRide() { + } + + public TaxiRide(Boolean isNightSurcharge, Long distanceInMile) { this.isNightSurcharge = isNightSurcharge; this.distanceInMile = distanceInMile; } - - + + public Boolean getIsNightSurcharge() { return isNightSurcharge; } diff --git a/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java b/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java index ec9c81187a..1176b31e4c 100644 --- a/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java +++ b/spring-rest/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java @@ -1,20 +1,14 @@ package com.baeldung.web.log.service; -import org.springframework.stereotype.Service; - import com.baeldung.web.log.data.TaxiRide; +import org.springframework.stereotype.Service; @Service public class TaxiFareCalculatorService { public String calculateFare(TaxiRide taxiRide) { - Long fare = 0l; - if (taxiRide.getIsNightSurcharge()) { - fare = taxiRide.getDistanceInMile() * 10 + 100; - } else { - fare = taxiRide.getDistanceInMile() * 10; - } - return String.valueOf(fare); + return String.valueOf((Long) (taxiRide.getIsNightSurcharge() + ? taxiRide.getDistanceInMile() * 10 + 100 + : taxiRide.getDistanceInMile() * 10)); } - } diff --git a/spring-rest/src/main/java/org/baeldung/config/MainApplication.java b/spring-rest/src/main/java/org/baeldung/config/MainApplication.java index e31fdfaaa9..36b021a537 100644 --- a/spring-rest/src/main/java/org/baeldung/config/MainApplication.java +++ b/spring-rest/src/main/java/org/baeldung/config/MainApplication.java @@ -12,5 +12,4 @@ public class MainApplication extends WebMvcConfigurerAdapter { public static void main(final String[] args) { SpringApplication.run(MainApplication.class, args); } - } \ No newline at end of file diff --git a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java index 4ed5c21b83..e4eb6d8875 100644 --- a/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java +++ b/spring-rest/src/main/java/org/baeldung/repository/HeavyResourceRepository.java @@ -1,11 +1,11 @@ package org.baeldung.repository; -import java.util.Map; - import org.baeldung.web.dto.HeavyResource; import org.baeldung.web.dto.HeavyResourceAddressOnly; -public class HeavyResourceRepository { +import java.util.Map; + +public class HeavyResourceRepository { public void save(HeavyResource heavyResource) { } @@ -21,6 +21,7 @@ public class HeavyResourceRepository { public void save(HeavyResource heavyResource, String id) { } + public void save(HeavyResourceAddressOnly partialUpdate, String id) { }