From d2d9db6ddadc6b31c0bef70fb74345cc5303f6c3 Mon Sep 17 00:00:00 2001 From: Sjmillington Date: Thu, 7 Nov 2019 17:58:42 +0000 Subject: [PATCH 1/2] [BAEL-18365] Spring rest move 2 --- spring-boot-data/README.md | 1 + .../PropertyEditorApplication.java | 0 .../PropertyEditorRestController.java | 0 .../propertyeditor/creditcard/CreditCard.java | 0 .../creditcard/CreditCardEditor.java | 0 .../editor/CustomExoticTypeEditor.java | 0 .../exotictype/model/ExoticType.java | 0 .../creditcard/CreditCardEditorUnitTest.java | 0 spring-boot-runtime/README.md | 6 +- spring-boot-runtime/pom.xml | 6 ++ .../changeport/CustomApplication.java | 0 .../changeport/ServerPortCustomizer.java | 0 .../main/java/com/baeldung/cors/Account.java | 0 .../com/baeldung/cors/AccountController.java | 0 .../sampleapp/config/MainApplication.java | 15 ++++ .../sampleapp/config/RestClientConfig.java | 29 +++++++ .../baeldung/sampleapp/config/WebConfig.java | 42 +++++++++ ...RestTemplateHeaderModifierInterceptor.java | 18 ++++ .../repository/HeavyResourceRepository.java | 28 ++++++ .../BarMappingExamplesController.java | 47 ++++++++++ .../web/controller/CompanyController.java | 17 ++++ .../controller/DeferredResultController.java | 85 +++++++++++++++++++ .../controller/HeavyResourceController.java | 42 +++++++++ .../web/controller/ItemController.java | 39 +++++++++ .../web/controller/MyFooController.java | 85 +++++++++++++++++++ .../web/controller/PactController.java | 33 +++++++ .../web/controller/SimplePostController.java | 74 ++++++++++++++++ .../mediatypes/CustomMediaTypeController.java | 25 ++++++ .../redirect/RedirectController.java | 68 +++++++++++++++ .../sampleapp/web/dto/BaeldungItem.java | 13 +++ .../sampleapp/web/dto/BaeldungItemV2.java | 14 +++ .../baeldung/sampleapp/web/dto/Company.java | 38 +++++++++ .../com/baeldung/sampleapp/web/dto/Foo.java | 42 +++++++++ .../sampleapp/web/dto/HeavyResource.java | 62 ++++++++++++++ .../web/dto/HeavyResourceAddressOnly.java | 31 +++++++ .../HeavyResourceAddressPartialUpdate.java | 31 +++++++ .../com/baeldung/sampleapp/web/dto/Item.java | 36 ++++++++ .../sampleapp/web/dto/ItemManager.java | 9 ++ .../baeldung/sampleapp/web/dto/PactDto.java | 33 +++++++ .../com/baeldung/sampleapp/web/dto/Views.java | 9 ++ .../exception/ResourceNotFoundException.java | 8 ++ .../com/baeldung/web/log/app/Application.java | 2 + .../log/app/TaxiFareRequestInterceptor.java | 0 .../config/CustomeRequestLoggingFilter.java | 0 .../config/RequestLoggingFilterConfig.java | 0 .../web/log/config/TaxiFareMVCConfig.java | 0 .../log/controller/TaxiFareController.java | 0 .../com/baeldung/web/log/data/RateCard.java | 0 .../com/baeldung/web/log/data/TaxiRide.java | 0 .../service/TaxiFareCalculatorService.java | 0 .../web/log/util/RequestLoggingUtil.java | 0 .../main/resources/application-log.properties | 2 + .../resources/application-logging.properties | 1 + .../src/main/webapp/WEB-INF/api-servlet.xml | 0 .../src/main/webapp/WEB-INF/company.html | 0 .../src/main/webapp/WEB-INF/spring-views.xml | 0 .../src/main/webapp/WEB-INF/web.xml | 0 ...eavyResourceControllerIntegrationTest.java | 0 .../TaxiFareControllerIntegrationTest.java | 4 +- spring-rest-full/README.md | 1 - spring-rest-simple/README.md | 2 +- spring-rest/.gitignore | 2 +- spring-rest/README.md | 3 - .../SpringContextIntegrationTest.java | 10 +-- .../java/com/baeldung/SpringContextTest.java | 11 +-- 65 files changed, 1001 insertions(+), 23 deletions(-) rename {spring-resttemplate => spring-boot-data}/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java (100%) rename {spring-resttemplate => spring-boot-data}/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java (100%) rename {spring-resttemplate => spring-boot-data}/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java (100%) rename {spring-resttemplate => spring-boot-data}/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java (100%) rename {spring-resttemplate => spring-boot-data}/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java (100%) rename {spring-resttemplate => spring-boot-data}/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java (100%) rename {spring-resttemplate => spring-boot-data}/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorUnitTest.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/changeport/CustomApplication.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/changeport/ServerPortCustomizer.java (100%) rename {spring-rest-simple => spring-boot-runtime}/src/main/java/com/baeldung/cors/Account.java (100%) rename {spring-rest-simple => spring-boot-runtime}/src/main/java/com/baeldung/cors/AccountController.java (100%) create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MainApplication.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/RestClientConfig.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/WebConfig.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/interceptors/RestTemplateHeaderModifierInterceptor.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/repository/HeavyResourceRepository.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/BarMappingExamplesController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/CompanyController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/DeferredResultController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/HeavyResourceController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/ItemController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/PactController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/mediatypes/CustomMediaTypeController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/redirect/RedirectController.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItem.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItemV2.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Company.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResource.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressOnly.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressPartialUpdate.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Item.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/ItemManager.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/PactDto.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Views.java create mode 100644 spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/exception/ResourceNotFoundException.java rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/app/Application.java (94%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/data/RateCard.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/data/TaxiRide.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java (100%) create mode 100644 spring-boot-runtime/src/main/resources/application-log.properties rename {spring-rest => spring-boot-runtime}/src/main/webapp/WEB-INF/api-servlet.xml (100%) rename {spring-rest => spring-boot-runtime}/src/main/webapp/WEB-INF/company.html (100%) rename {spring-rest => spring-boot-runtime}/src/main/webapp/WEB-INF/spring-views.xml (100%) rename {spring-rest => spring-boot-runtime}/src/main/webapp/WEB-INF/web.xml (100%) rename {spring-resttemplate => spring-boot-runtime}/src/test/java/com/baeldung/web/controller/HeavyResourceControllerIntegrationTest.java (100%) rename {spring-resttemplate => spring-boot-runtime}/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java (95%) diff --git a/spring-boot-data/README.md b/spring-boot-data/README.md index eb3cd5bbaa..f023bb772f 100644 --- a/spring-boot-data/README.md +++ b/spring-boot-data/README.md @@ -8,3 +8,4 @@ This module contains articles about Spring Boot with Spring Data - [Rendering Exceptions in JSON with Spring](https://www.baeldung.com/spring-exceptions-json) - [Disable Spring Data Auto Configuration](https://www.baeldung.com/spring-data-disable-auto-config) - [Repositories with Multiple Spring Data Modules](https://www.baeldung.com/spring-multiple-data-modules) +- [Spring Custom Property Editor](https://www.baeldung.com/spring-mvc-custom-property-editor) diff --git a/spring-resttemplate/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java b/spring-boot-data/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java rename to spring-boot-data/src/main/java/com/baeldung/propertyeditor/PropertyEditorApplication.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java b/spring-boot-data/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java rename to spring-boot-data/src/main/java/com/baeldung/propertyeditor/PropertyEditorRestController.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java b/spring-boot-data/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java rename to spring-boot-data/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCard.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java b/spring-boot-data/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java rename to spring-boot-data/src/main/java/com/baeldung/propertyeditor/creditcard/CreditCardEditor.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java b/spring-boot-data/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java rename to spring-boot-data/src/main/java/com/baeldung/propertyeditor/exotictype/editor/CustomExoticTypeEditor.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java b/spring-boot-data/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java rename to spring-boot-data/src/main/java/com/baeldung/propertyeditor/exotictype/model/ExoticType.java diff --git a/spring-resttemplate/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorUnitTest.java b/spring-boot-data/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorUnitTest.java similarity index 100% rename from spring-resttemplate/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorUnitTest.java rename to spring-boot-data/src/test/java/com/baeldung/propertyeditor/creditcard/CreditCardEditorUnitTest.java diff --git a/spring-boot-runtime/README.md b/spring-boot-runtime/README.md index d3dc19493d..a544faf830 100644 --- a/spring-boot-runtime/README.md +++ b/spring-boot-runtime/README.md @@ -7,4 +7,8 @@ This module contains articles about administering a Spring Boot runtime - [Programmatically Restarting a Spring Boot Application](https://www.baeldung.com/java-restart-spring-boot-app) - [Logging HTTP Requests with Spring Boot Actuator HTTP Tracing](https://www.baeldung.com/spring-boot-actuator-http) - [How to Disable Console Logging in Spring Boot](https://www.baeldung.com/spring-boot-disable-console-logging) - - [Spring Boot Embedded Tomcat Logs](https://www.baeldung.com/spring-boot-embedded-tomcat-logs) \ No newline at end of file + - [Spring Boot Embedded Tomcat Logs](https://www.baeldung.com/spring-boot-embedded-tomcat-logs) + - [How to Change the Default Port in Spring Boot](https://www.baeldung.com/spring-boot-change-port) + - [Project Configuration with Spring](https://www.baeldung.com/project-configuration-with-spring) + - [CORS with Spring](https://www.baeldung.com/spring-cors) + - [Spring – Log Incoming Requests](https://www.baeldung.com/spring-http-logging) \ No newline at end of file diff --git a/spring-boot-runtime/pom.xml b/spring-boot-runtime/pom.xml index dca9b47410..f5c98f0fde 100644 --- a/spring-boot-runtime/pom.xml +++ b/spring-boot-runtime/pom.xml @@ -32,6 +32,12 @@ spring-boot-starter-test test + + + commons-io + commons-io + 2.6 + org.springframework.boot diff --git a/spring-resttemplate/src/main/java/com/baeldung/changeport/CustomApplication.java b/spring-boot-runtime/src/main/java/com/baeldung/changeport/CustomApplication.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/changeport/CustomApplication.java rename to spring-boot-runtime/src/main/java/com/baeldung/changeport/CustomApplication.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/changeport/ServerPortCustomizer.java b/spring-boot-runtime/src/main/java/com/baeldung/changeport/ServerPortCustomizer.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/changeport/ServerPortCustomizer.java rename to spring-boot-runtime/src/main/java/com/baeldung/changeport/ServerPortCustomizer.java diff --git a/spring-rest-simple/src/main/java/com/baeldung/cors/Account.java b/spring-boot-runtime/src/main/java/com/baeldung/cors/Account.java similarity index 100% rename from spring-rest-simple/src/main/java/com/baeldung/cors/Account.java rename to spring-boot-runtime/src/main/java/com/baeldung/cors/Account.java diff --git a/spring-rest-simple/src/main/java/com/baeldung/cors/AccountController.java b/spring-boot-runtime/src/main/java/com/baeldung/cors/AccountController.java similarity index 100% rename from spring-rest-simple/src/main/java/com/baeldung/cors/AccountController.java rename to spring-boot-runtime/src/main/java/com/baeldung/cors/AccountController.java diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MainApplication.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MainApplication.java new file mode 100644 index 0000000000..507f340e9d --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/MainApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.sampleapp.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@EnableAutoConfiguration +@ComponentScan("com.baeldung.sampleapp") +public class MainApplication implements WebMvcConfigurer { + + public static void main(final String[] args) { + SpringApplication.run(MainApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/RestClientConfig.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/RestClientConfig.java new file mode 100644 index 0000000000..cbaa21f4ca --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/RestClientConfig.java @@ -0,0 +1,29 @@ +package com.baeldung.sampleapp.config; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.util.CollectionUtils; +import org.springframework.web.client.RestTemplate; + +import com.baeldung.sampleapp.interceptors.RestTemplateHeaderModifierInterceptor; + +@Configuration +public class RestClientConfig { + + @Bean + public RestTemplate restTemplate() { + RestTemplate restTemplate = new RestTemplate(); + + List interceptors = restTemplate.getInterceptors(); + if (CollectionUtils.isEmpty(interceptors)) { + interceptors = new ArrayList(); + } + interceptors.add(new RestTemplateHeaderModifierInterceptor()); + restTemplate.setInterceptors(interceptors); + return restTemplate; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/WebConfig.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/WebConfig.java new file mode 100644 index 0000000000..dc4fb9c695 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/config/WebConfig.java @@ -0,0 +1,42 @@ +package com.baeldung.sampleapp.config; + +import java.text.SimpleDateFormat; +import java.util.List; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/* + * Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml + */ +@Configuration +@EnableWebMvc +@ComponentScan({ "com.baeldung.sampleapp.web" }) +public class WebConfig implements WebMvcConfigurer { + + public WebConfig() { + super(); + } + + /* + @Override + public void configureMessageConverters(final List> messageConverters) { + final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); + builder.indentOutput(true) + .dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm")); + messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build())); + // messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())); + + // messageConverters.add(new MappingJackson2HttpMessageConverter()); + + // messageConverters.add(new ProtobufHttpMessageConverter()); + + } + + */ +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/interceptors/RestTemplateHeaderModifierInterceptor.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/interceptors/RestTemplateHeaderModifierInterceptor.java new file mode 100644 index 0000000000..9ebe1553a5 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/interceptors/RestTemplateHeaderModifierInterceptor.java @@ -0,0 +1,18 @@ +package com.baeldung.sampleapp.interceptors; + +import java.io.IOException; + +import org.springframework.http.HttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; + +public class RestTemplateHeaderModifierInterceptor implements ClientHttpRequestInterceptor { + + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { + ClientHttpResponse response = execution.execute(request, body); + response.getHeaders().add("Foo", "bar"); + return response; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/repository/HeavyResourceRepository.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/repository/HeavyResourceRepository.java new file mode 100644 index 0000000000..ea9541c31a --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/repository/HeavyResourceRepository.java @@ -0,0 +1,28 @@ +package com.baeldung.sampleapp.repository; + +import java.util.Map; + +import com.baeldung.sampleapp.web.dto.HeavyResource; +import com.baeldung.sampleapp.web.dto.HeavyResourceAddressOnly; + +public class HeavyResourceRepository { + + public void save(HeavyResource heavyResource) { + } + + public void save(HeavyResourceAddressOnly partialUpdate) { + + } + + public void save(Map updates, String id) { + + } + + public void save(HeavyResource heavyResource, String id) { + + } + + public void save(HeavyResourceAddressOnly partialUpdate, String id) { + + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/BarMappingExamplesController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/BarMappingExamplesController.java new file mode 100644 index 0000000000..c6b8d23944 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/BarMappingExamplesController.java @@ -0,0 +1,47 @@ +package com.baeldung.sampleapp.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping(value = "/ex") +public class BarMappingExamplesController { + + public BarMappingExamplesController() { + super(); + } + + // API + + // with @RequestParam + + @RequestMapping(value = "/bars") + @ResponseBody + public String getBarBySimplePathWithRequestParam(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + @RequestMapping(value = "/bars", params = "id") + @ResponseBody + public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + @RequestMapping(value = "/bars", params = { "id", "second" }) + @ResponseBody + public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + // with @PathVariable + + @RequestMapping(value = "/bars/{numericId:[\\d]+}") + @ResponseBody + public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) { + return "Get a specific Bar with id=" + numericId; + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/CompanyController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/CompanyController.java new file mode 100644 index 0000000000..bfda2fe0d6 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/CompanyController.java @@ -0,0 +1,17 @@ +package com.baeldung.sampleapp.web.controller; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.sampleapp.web.dto.Company; + +@RestController +public class CompanyController { + + @RequestMapping(value = "/companyRest", produces = MediaType.APPLICATION_JSON_VALUE) + public Company getCompanyRest() { + final Company company = new Company(1, "Xpto"); + return company; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/DeferredResultController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/DeferredResultController.java new file mode 100644 index 0000000000..8f4eb3218a --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/DeferredResultController.java @@ -0,0 +1,85 @@ +package com.baeldung.sampleapp.web.controller; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.async.DeferredResult; + +@RestController +public class DeferredResultController { + + private final static Logger LOG = LoggerFactory.getLogger(DeferredResultController.class); + + @GetMapping("/async-deferredresult") + public DeferredResult> handleReqDefResult(Model model) { + LOG.info("Received request"); + DeferredResult> deferredResult = new DeferredResult<>(); + + deferredResult.onCompletion(() -> LOG.info("Processing complete")); + + CompletableFuture.supplyAsync(() -> { + LOG.info("Processing in separate thread"); + try { + Thread.sleep(6000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "OK"; + }) + .whenCompleteAsync((result, exc) -> deferredResult.setResult(ResponseEntity.ok(result))); + + LOG.info("Servlet thread freed"); + return deferredResult; + } + + @GetMapping("/process-blocking") + public ResponseEntity handleReqSync(Model model) { + // ... + return ResponseEntity.ok("ok"); + } + + @GetMapping("/async-deferredresult-timeout") + public DeferredResult> handleReqWithTimeouts(Model model) { + LOG.info("Received async request with a configured timeout"); + DeferredResult> deferredResult = new DeferredResult<>(500l); + deferredResult.onTimeout(() -> deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT) + .body("Request timeout occurred."))); + + CompletableFuture.supplyAsync(() -> { + LOG.info("Processing in separate thread"); + try { + Thread.sleep(6000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "error"; + }) + .whenCompleteAsync((result, exc) -> deferredResult.setResult(ResponseEntity.ok(result))); + LOG.info("servlet thread freed"); + return deferredResult; + } + + @GetMapping("/async-deferredresult-error") + public DeferredResult> handleAsyncFailedRequest(Model model) { + LOG.info("Received async request with a configured error handler"); + DeferredResult> deferredResult = new DeferredResult<>(); + deferredResult.onError(new Consumer() { + @Override + public void accept(Throwable t) { + deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An error occurred.")); + } + + }); + LOG.info("servlet thread freed"); + return deferredResult; + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/HeavyResourceController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/HeavyResourceController.java new file mode 100644 index 0000000000..8156fc14a9 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/HeavyResourceController.java @@ -0,0 +1,42 @@ +package com.baeldung.sampleapp.web.controller; + + +import java.util.Map; + +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.sampleapp.repository.HeavyResourceRepository; +import com.baeldung.sampleapp.web.dto.HeavyResource; +import com.baeldung.sampleapp.web.dto.HeavyResourceAddressOnly; + +@RestController +public class HeavyResourceController { + + private HeavyResourceRepository heavyResourceRepository = new HeavyResourceRepository(); + + @RequestMapping(value = "/heavyresource/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity saveResource(@RequestBody HeavyResource heavyResource, @PathVariable("id") String id) { + heavyResourceRepository.save(heavyResource, id); + return ResponseEntity.ok("resource saved"); + } + + @RequestMapping(value = "/heavyresource/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity partialUpdateName(@RequestBody HeavyResourceAddressOnly partialUpdate, @PathVariable("id") String id) { + heavyResourceRepository.save(partialUpdate, id); + return ResponseEntity.ok("resource address updated"); + } + + @RequestMapping(value = "/heavyresource2/{id}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity partialUpdateGeneric(@RequestBody Map updates, + @PathVariable("id") String id) { + heavyResourceRepository.save(updates, id); + return ResponseEntity.ok("resource updated"); + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/ItemController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/ItemController.java new file mode 100644 index 0000000000..69bd458968 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/ItemController.java @@ -0,0 +1,39 @@ +package com.baeldung.sampleapp.web.controller; + +import java.util.Date; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.sampleapp.web.dto.Item; +import com.baeldung.sampleapp.web.dto.ItemManager; +import com.baeldung.sampleapp.web.dto.Views; +import com.fasterxml.jackson.annotation.JsonView; + +@RestController +public class ItemController { + + @JsonView(Views.Public.class) + @RequestMapping("/items/{id}") + public Item getItemPublic(@PathVariable final int id) { + return ItemManager.getById(id); + } + + @JsonView(Views.Internal.class) + @RequestMapping("/items/internal/{id}") + public Item getItemInternal(@PathVariable final int id) { + return ItemManager.getById(id); + } + + @RequestMapping("/date") + public Date getCurrentDate() throws Exception { + return new Date(); + } + + @RequestMapping("/delay/{seconds}") + public void getCurrentTime(@PathVariable final int seconds) throws Exception { + + Thread.sleep(seconds * 1000); + } +} \ No newline at end of file diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java new file mode 100644 index 0000000000..11ea5b70c9 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/MyFooController.java @@ -0,0 +1,85 @@ +package com.baeldung.sampleapp.web.controller; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +import com.baeldung.sampleapp.web.dto.Foo; +import com.baeldung.sampleapp.web.exception.ResourceNotFoundException; + +@Controller +@RequestMapping(value = "/foos") +public class MyFooController { + + private final Map myfoos; + + public MyFooController() { + super(); + myfoos = new HashMap(); + myfoos.put(1L, new Foo(1L, "sample foo")); + } + + // API - read + + @RequestMapping(method = RequestMethod.GET, produces = { "application/json" }) + @ResponseBody + public Collection findAll() { + return myfoos.values(); + } + + @RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = { "application/json" }) + @ResponseBody + public Foo findById(@PathVariable final long id) { + final Foo foo = myfoos.get(id); + if (foo == null) { + throw new ResourceNotFoundException(); + } + return foo; + } + + // API - write + + @RequestMapping(method = RequestMethod.PUT, value = "/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo updateFoo(@PathVariable("id") final long id, @RequestBody final Foo foo) { + myfoos.put(id, foo); + return foo; + } + + @RequestMapping(method = RequestMethod.PATCH, value = "/{id}") + @ResponseStatus(HttpStatus.OK) + public void updateFoo2(@PathVariable("id") final long id, @RequestBody final Foo foo) { + myfoos.put(id, foo); + } + + @RequestMapping(method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) + @ResponseBody + public Foo createFoo(@RequestBody final Foo foo, HttpServletResponse response) { + myfoos.put(foo.getId(), foo); + response.setHeader("Location", ServletUriComponentsBuilder.fromCurrentRequest() + .path("/" + foo.getId()) + .toUriString()); + return foo; + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") + @ResponseStatus(HttpStatus.OK) + public void deleteById(@PathVariable final long id) { + myfoos.remove(id); + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/PactController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/PactController.java new file mode 100644 index 0000000000..0f5d7f1acb --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/PactController.java @@ -0,0 +1,33 @@ +package com.baeldung.sampleapp.web.controller; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.sampleapp.web.dto.PactDto; + +@RestController +public class PactController { + + List pacts = new ArrayList<>(); + + @GetMapping(value = "/pact", produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public PactDto getPact() { + return new PactDto(true, "tom"); + } + + @PostMapping("/pact") + @ResponseStatus(HttpStatus.CREATED) + public void createPact(PactDto pact) { + pacts.add(pact); + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java new file mode 100644 index 0000000000..7b57d35088 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/SimplePostController.java @@ -0,0 +1,74 @@ +package com.baeldung.sampleapp.web.controller; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import com.baeldung.sampleapp.web.dto.Foo; + +// used to test HttpClientPostingTest +@RestController +public class SimplePostController { + + @RequestMapping(value = "/users", method = RequestMethod.POST) + public String postUser(@RequestParam final String username, @RequestParam final String password) { + return "Success" + username; + } + + @RequestMapping(value = "/users/detail", method = RequestMethod.POST) + public String postUserDetail(@RequestBody final Foo entity) { + return "Success" + entity.getId(); + } + + @RequestMapping(value = "/users/multipart", method = RequestMethod.POST) + public String uploadFile(@RequestParam final String username, @RequestParam final String password, @RequestParam("file") final MultipartFile file) { + if (!file.isEmpty()) { + try { + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); + final String fileName = dateFormat.format(new Date()); + final File fileServer = new File(fileName); + fileServer.createNewFile(); + final byte[] bytes = file.getBytes(); + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); + stream.write(bytes); + stream.close(); + return "You successfully uploaded " + username; + } catch (final Exception e) { + return "You failed to upload " + e.getMessage(); + } + } else { + return "You failed to upload because the file was empty."; + } + } + + @RequestMapping(value = "/users/upload", method = RequestMethod.POST) + public String postMultipart(@RequestParam("file") final MultipartFile file) { + if (!file.isEmpty()) { + try { + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); + final String fileName = dateFormat.format(new Date()); + final File fileServer = new File(fileName); + fileServer.createNewFile(); + final byte[] bytes = file.getBytes(); + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); + stream.write(bytes); + stream.close(); + return "You successfully uploaded "; + } catch (final Exception e) { + return "You failed to upload " + e.getMessage(); + } + } else { + return "You failed to upload because the file was empty."; + } + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/mediatypes/CustomMediaTypeController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/mediatypes/CustomMediaTypeController.java new file mode 100644 index 0000000000..fc73bade87 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/mediatypes/CustomMediaTypeController.java @@ -0,0 +1,25 @@ +package com.baeldung.sampleapp.web.controller.mediatypes; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.sampleapp.web.dto.BaeldungItem; +import com.baeldung.sampleapp.web.dto.BaeldungItemV2; + +@RestController +@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json") +public class CustomMediaTypeController { + + @RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v1+json") + public @ResponseBody BaeldungItem getItem(@PathVariable("id") String id) { + return new BaeldungItem("itemId1"); + } + + @RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v2+json") + public @ResponseBody BaeldungItemV2 getItemSecondAPIVersion(@PathVariable("id") String id) { + return new BaeldungItemV2("itemName"); + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/redirect/RedirectController.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/redirect/RedirectController.java new file mode 100644 index 0000000000..321f3be3ef --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/controller/redirect/RedirectController.java @@ -0,0 +1,68 @@ +package com.baeldung.sampleapp.web.controller.redirect; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.view.RedirectView; + +@Controller +@RequestMapping("/") +public class RedirectController { + + @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) + public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { + model.addAttribute("attribute", "redirectWithXMLConfig"); + return new ModelAndView("RedirectedUrl", model); + } + + @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectPrefix"); + return new ModelAndView("redirect:/redirectedUrl", model); + } + + @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) + public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { + redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); + redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); + return new RedirectView("redirectedUrl"); + } + + @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) + public RedirectView redirectWithUsingRedirectView(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectView"); + return new RedirectView("redirectedUrl"); + } + + @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithForwardPrefix"); + return new ModelAndView("forward:/redirectedUrl", model); + } + + @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) + public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + model.addAttribute("redirectionAttribute", flashAttribute); + return new ModelAndView("redirection", model); + } + + @RequestMapping(value = "/redirectPostToPost", method = RequestMethod.POST) + public ModelAndView redirectPostToPost(HttpServletRequest request) { + request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT); + return new ModelAndView("redirect:/redirectedPostToPost"); + } + + @RequestMapping(value = "/redirectedPostToPost", method = RequestMethod.POST) + public ModelAndView redirectedPostToPost() { + return new ModelAndView("redirection"); + } + +} \ No newline at end of file diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItem.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItem.java new file mode 100644 index 0000000000..807a254cfc --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItem.java @@ -0,0 +1,13 @@ +package com.baeldung.sampleapp.web.dto; + +public class BaeldungItem { + private final String itemId; + + public BaeldungItem(String itemId) { + this.itemId = itemId; + } + + public String getItemId() { + return itemId; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItemV2.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItemV2.java new file mode 100644 index 0000000000..f84591ea43 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/BaeldungItemV2.java @@ -0,0 +1,14 @@ +package com.baeldung.sampleapp.web.dto; + + +public class BaeldungItemV2 { + private final String itemName; + + public BaeldungItemV2(String itemName) { + this.itemName = itemName; + } + + public String getItemName() { + return itemName; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Company.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Company.java new file mode 100644 index 0000000000..6cfcc079d9 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Company.java @@ -0,0 +1,38 @@ +package com.baeldung.sampleapp.web.dto; + +public class Company { + + private long id; + private String name; + + public Company() { + super(); + } + + public Company(final long id, final String name) { + this.id = id; + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + @Override + public String toString() { + return "Company [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java new file mode 100644 index 0000000000..de1d76ed92 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Foo.java @@ -0,0 +1,42 @@ +package com.baeldung.sampleapp.web.dto; + +public class Foo { + private long id; + private String name; + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + + this.name = name; + } + + public Foo(final long id, final String name) { + super(); + + this.id = id; + this.name = name; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResource.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResource.java new file mode 100644 index 0000000000..2821341888 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResource.java @@ -0,0 +1,62 @@ +package com.baeldung.sampleapp.web.dto; + + +public class HeavyResource { + private Integer id; + private String name; + private String surname; + private Integer age; + private String address; + + + public HeavyResource() { + } + + public HeavyResource(Integer id, String name, String surname, Integer age, String address) { + this.id = id; + this.name = name; + this.surname = surname; + this.age = age; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressOnly.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressOnly.java new file mode 100644 index 0000000000..01ee6e7dd4 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressOnly.java @@ -0,0 +1,31 @@ +package com.baeldung.sampleapp.web.dto; + + +public class HeavyResourceAddressOnly { + private Integer id; + private String address; + + public HeavyResourceAddressOnly() { + } + + public HeavyResourceAddressOnly(Integer id, String address) { + this.id = id; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressPartialUpdate.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressPartialUpdate.java new file mode 100644 index 0000000000..1832a1a58b --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/HeavyResourceAddressPartialUpdate.java @@ -0,0 +1,31 @@ +package com.baeldung.sampleapp.web.dto; + + +public class HeavyResourceAddressPartialUpdate { + private Integer id; + private String address; + + public HeavyResourceAddressPartialUpdate() { + } + + public HeavyResourceAddressPartialUpdate(Integer id, String address) { + this.id = id; + this.address = address; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Item.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Item.java new file mode 100644 index 0000000000..a4fcef5dce --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Item.java @@ -0,0 +1,36 @@ +package com.baeldung.sampleapp.web.dto; + +import com.fasterxml.jackson.annotation.JsonView; + +public class Item { + @JsonView(Views.Public.class) + public int id; + + @JsonView(Views.Public.class) + public String itemName; + + @JsonView(Views.Internal.class) + public String ownerName; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final String ownerName) { + this.id = id; + this.itemName = itemName; + this.ownerName = ownerName; + } + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public String getOwnerName() { + return ownerName; + } +} \ No newline at end of file diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/ItemManager.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/ItemManager.java new file mode 100644 index 0000000000..0009c0180b --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/ItemManager.java @@ -0,0 +1,9 @@ +package com.baeldung.sampleapp.web.dto; + +public class ItemManager { + + public static Item getById(final int id) { + final Item item = new Item(2, "book", "John"); + return item; + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/PactDto.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/PactDto.java new file mode 100644 index 0000000000..e184119611 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/PactDto.java @@ -0,0 +1,33 @@ +package com.baeldung.sampleapp.web.dto; + +public class PactDto { + + private boolean condition; + private String name; + + public PactDto() { + } + + public PactDto(boolean condition, String name) { + super(); + this.condition = condition; + this.name = name; + } + + public boolean isCondition() { + return condition; + } + + public void setCondition(boolean condition) { + this.condition = condition; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Views.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Views.java new file mode 100644 index 0000000000..e2d83fda22 --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/dto/Views.java @@ -0,0 +1,9 @@ +package com.baeldung.sampleapp.web.dto; + +public class Views { + public static class Public { + } + + public static class Internal extends Public { + } +} diff --git a/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/exception/ResourceNotFoundException.java b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/exception/ResourceNotFoundException.java new file mode 100644 index 0000000000..69532f196d --- /dev/null +++ b/spring-boot-runtime/src/main/java/com/baeldung/sampleapp/web/exception/ResourceNotFoundException.java @@ -0,0 +1,8 @@ +package com.baeldung.sampleapp.web.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends RuntimeException { +} diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/app/Application.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java similarity index 94% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/app/Application.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java index 6e2607339c..f20fba737a 100644 --- a/spring-resttemplate/src/main/java/com/baeldung/web/log/app/Application.java +++ b/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java @@ -11,6 +11,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.context.annotation.PropertySource; import com.baeldung.web.log.config.CustomeRequestLoggingFilter; @@ -18,6 +19,7 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer @EnableAutoConfiguration @ComponentScan("com.baeldung.web.log") +@PropertySource("application-log.properties") @SpringBootApplication public class Application extends SpringBootServletInitializer { diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/controller/TaxiFareController.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/data/RateCard.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/data/RateCard.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/data/RateCard.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/data/RateCard.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/data/TaxiRide.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/data/TaxiRide.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/data/TaxiRide.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/data/TaxiRide.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/service/TaxiFareCalculatorService.java diff --git a/spring-resttemplate/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java b/spring-boot-runtime/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java similarity index 100% rename from spring-resttemplate/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java rename to spring-boot-runtime/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java diff --git a/spring-boot-runtime/src/main/resources/application-log.properties b/spring-boot-runtime/src/main/resources/application-log.properties new file mode 100644 index 0000000000..1a26e3ad99 --- /dev/null +++ b/spring-boot-runtime/src/main/resources/application-log.properties @@ -0,0 +1,2 @@ +server.port=8082 +server.servlet.context-path=/spring-rest \ No newline at end of file diff --git a/spring-boot-runtime/src/main/resources/application-logging.properties b/spring-boot-runtime/src/main/resources/application-logging.properties index 338251cf9b..d3bdaa00b7 100644 --- a/spring-boot-runtime/src/main/resources/application-logging.properties +++ b/spring-boot-runtime/src/main/resources/application-logging.properties @@ -20,3 +20,4 @@ spring.application.name=spring-boot-management server.port=8081 + diff --git a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml b/spring-boot-runtime/src/main/webapp/WEB-INF/api-servlet.xml similarity index 100% rename from spring-rest/src/main/webapp/WEB-INF/api-servlet.xml rename to spring-boot-runtime/src/main/webapp/WEB-INF/api-servlet.xml diff --git a/spring-rest/src/main/webapp/WEB-INF/company.html b/spring-boot-runtime/src/main/webapp/WEB-INF/company.html similarity index 100% rename from spring-rest/src/main/webapp/WEB-INF/company.html rename to spring-boot-runtime/src/main/webapp/WEB-INF/company.html diff --git a/spring-rest/src/main/webapp/WEB-INF/spring-views.xml b/spring-boot-runtime/src/main/webapp/WEB-INF/spring-views.xml similarity index 100% rename from spring-rest/src/main/webapp/WEB-INF/spring-views.xml rename to spring-boot-runtime/src/main/webapp/WEB-INF/spring-views.xml diff --git a/spring-rest/src/main/webapp/WEB-INF/web.xml b/spring-boot-runtime/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from spring-rest/src/main/webapp/WEB-INF/web.xml rename to spring-boot-runtime/src/main/webapp/WEB-INF/web.xml diff --git a/spring-resttemplate/src/test/java/com/baeldung/web/controller/HeavyResourceControllerIntegrationTest.java b/spring-boot-runtime/src/test/java/com/baeldung/web/controller/HeavyResourceControllerIntegrationTest.java similarity index 100% rename from spring-resttemplate/src/test/java/com/baeldung/web/controller/HeavyResourceControllerIntegrationTest.java rename to spring-boot-runtime/src/test/java/com/baeldung/web/controller/HeavyResourceControllerIntegrationTest.java diff --git a/spring-resttemplate/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java b/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java similarity index 95% rename from spring-resttemplate/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java rename to spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java index 1cc098abf1..602971635b 100644 --- a/spring-resttemplate/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java +++ b/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java @@ -22,7 +22,7 @@ public class TaxiFareControllerIntegrationTest { @Test public void givenRequest_whenFetchTaxiFareRateCard_thanOK() { - + String URL = "http://localhost:" + port + "/spring-rest"; TestRestTemplate testRestTemplate = new TestRestTemplate(); TaxiRide taxiRide = new TaxiRide(true, 10l); @@ -30,6 +30,6 @@ public class TaxiFareControllerIntegrationTest { URL + "/taxifare/calculate/", taxiRide, String.class); - assertThat(fare, equalTo("200")); + //assertThat(fare, equalTo("200")); } } \ No newline at end of file diff --git a/spring-rest-full/README.md b/spring-rest-full/README.md index df7856725f..70ab6b2c80 100644 --- a/spring-rest-full/README.md +++ b/spring-rest-full/README.md @@ -10,7 +10,6 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ### Relevant Articles: - [Integration Testing with the Maven Cargo plugin](https://www.baeldung.com/integration-testing-with-the-maven-cargo-plugin) -- [Project Configuration with Spring](https://www.baeldung.com/project-configuration-with-spring) - [Metrics for your Spring REST API](https://www.baeldung.com/spring-rest-api-metrics) ### Build the Project diff --git a/spring-rest-simple/README.md b/spring-rest-simple/README.md index 1ad32bf120..c2df4d9648 100644 --- a/spring-rest-simple/README.md +++ b/spring-rest-simple/README.md @@ -9,4 +9,4 @@ This module contains articles about REST APIs in Spring - [Spring RequestMapping](https://www.baeldung.com/spring-requestmapping) - [Spring and Apache FileUpload](https://www.baeldung.com/spring-apache-file-upload) - [Test a REST API with curl](https://www.baeldung.com/curl-rest) -- [CORS with Spring](https://www.baeldung.com/spring-cors) + diff --git a/spring-rest/.gitignore b/spring-rest/.gitignore index 83c05e60c8..2661c0de1e 100644 --- a/spring-rest/.gitignore +++ b/spring-rest/.gitignore @@ -4,7 +4,7 @@ /target /neoDb* /data -/src/main/webapp/WEB-INF/classes +/spring-boot-runtime/src/main/webapp/WEB-INF/classes */META-INF/* # Packaged files # diff --git a/spring-rest/README.md b/spring-rest/README.md index af054b2dcf..3ea9361169 100644 --- a/spring-rest/README.md +++ b/spring-rest/README.md @@ -12,9 +12,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Guide to UriComponentsBuilder in Spring](https://www.baeldung.com/spring-uricomponentsbuilder) - [A Custom Media Type for a Spring REST API](https://www.baeldung.com/spring-rest-custom-media-type) - [HTTP PUT vs HTTP PATCH in a REST API](https://www.baeldung.com/http-put-patch-difference-spring) -- [Spring – Log Incoming Requests](https://www.baeldung.com/spring-http-logging) -- [How to Change the Default Port in Spring Boot](https://www.baeldung.com/spring-boot-change-port) - [Guide to DeferredResult in Spring](https://www.baeldung.com/spring-deferred-result) -- [Spring Custom Property Editor](https://www.baeldung.com/spring-mvc-custom-property-editor) - [How to Set a Header on a Response with Spring 5](https://www.baeldung.com/spring-response-header) - [Download an Image or a File with Spring MVC](https://www.baeldung.com/spring-controller-return-image-file) diff --git a/spring-resttemplate/src/main/java/com/baeldung/SpringContextIntegrationTest.java b/spring-resttemplate/src/main/java/com/baeldung/SpringContextIntegrationTest.java index 3c2cde4268..6801b694c4 100644 --- a/spring-resttemplate/src/main/java/com/baeldung/SpringContextIntegrationTest.java +++ b/spring-resttemplate/src/main/java/com/baeldung/SpringContextIntegrationTest.java @@ -5,17 +5,13 @@ import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.changeport.CustomApplication; import com.baeldung.produceimage.ImageApplication; -import com.baeldung.propertyeditor.PropertyEditorApplication; import com.baeldung.responseheaders.ResponseHeadersApplication; -import com.baeldung.sampleapp.config.MainApplication; -import com.baeldung.web.log.app.Application; @RunWith(SpringRunner.class) -@SpringBootTest(classes = { CustomApplication.class, ImageApplication.class, PropertyEditorApplication.class, - ResponseHeadersApplication.class, Application.class, com.baeldung.web.upload.app.UploadApplication.class, - MainApplication.class}) +@SpringBootTest(classes = { ImageApplication.class, + ResponseHeadersApplication.class, com.baeldung.web.upload.app.UploadApplication.class, + }) public class SpringContextIntegrationTest { @Test diff --git a/spring-resttemplate/src/main/java/com/baeldung/SpringContextTest.java b/spring-resttemplate/src/main/java/com/baeldung/SpringContextTest.java index 0f39fc3983..19d5eabd2b 100644 --- a/spring-resttemplate/src/main/java/com/baeldung/SpringContextTest.java +++ b/spring-resttemplate/src/main/java/com/baeldung/SpringContextTest.java @@ -5,17 +5,14 @@ import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.changeport.CustomApplication; import com.baeldung.produceimage.ImageApplication; -import com.baeldung.propertyeditor.PropertyEditorApplication; import com.baeldung.responseheaders.ResponseHeadersApplication; -import com.baeldung.sampleapp.config.MainApplication; -import com.baeldung.web.log.app.Application; @RunWith(SpringRunner.class) -@SpringBootTest(classes = { CustomApplication.class, ImageApplication.class, PropertyEditorApplication.class, - ResponseHeadersApplication.class, Application.class, com.baeldung.web.upload.app.UploadApplication.class, - MainApplication.class}) +@SpringBootTest(classes = { ImageApplication.class, + ResponseHeadersApplication.class, + com.baeldung.web.upload.app.UploadApplication.class, + }) public class SpringContextTest { @Test From 51e32cca925354e7821ef2e39984ceed1e8cf394 Mon Sep 17 00:00:00 2001 From: Sjmillington Date: Sat, 23 Nov 2019 13:06:06 +0000 Subject: [PATCH 2/2] added SecurityConfig --- spring-boot-runtime/pom.xml | 4 ++++ .../TaxiFareControllerIntegrationTest.java | 21 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spring-boot-runtime/pom.xml b/spring-boot-runtime/pom.xml index f5c98f0fde..772fd20d9e 100644 --- a/spring-boot-runtime/pom.xml +++ b/spring-boot-runtime/pom.xml @@ -63,6 +63,10 @@ spring-boot-admin-starter-client ${spring-boot-admin-starter-client.version} + + org.springframework.security + spring-security-test + com.h2database diff --git a/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java b/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java index 602971635b..97d669d3fa 100644 --- a/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java +++ b/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java @@ -7,14 +7,17 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.context.annotation.Configuration; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import com.baeldung.web.log.app.Application; import com.baeldung.web.log.data.TaxiRide; @RunWith(SpringRunner.class) -@SpringBootTest(classes = { Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = { Application.class, TaxiFareControllerIntegrationTest.SecurityConfig.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class TaxiFareControllerIntegrationTest { @LocalServerPort @@ -23,6 +26,7 @@ public class TaxiFareControllerIntegrationTest { @Test public void givenRequest_whenFetchTaxiFareRateCard_thanOK() { + System.out.println(port); String URL = "http://localhost:" + port + "/spring-rest"; TestRestTemplate testRestTemplate = new TestRestTemplate(); TaxiRide taxiRide = new TaxiRide(true, 10l); @@ -30,6 +34,19 @@ public class TaxiFareControllerIntegrationTest { URL + "/taxifare/calculate/", taxiRide, String.class); - //assertThat(fare, equalTo("200")); + assertThat(fare, equalTo("200")); + } + + @Configuration + static class SecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + System.out.println("security being set"); + http + .authorizeRequests() + .anyRequest().permitAll() + .and() + .csrf().disable(); + } } } \ No newline at end of file