From 59ff2bb22b126458a313ca5fc0075242ad396938 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 5 Feb 2017 20:39:52 +0100 Subject: [PATCH] Spring Boot REST Client --- REST/SpringBoot/pom.xml | 1 + .../web-services-spring-rest-client/README | 1 + .../web-services-spring-rest-client/pom.xml | 85 +++++++++++----------- .../java/de/spring/webservices/main/MainTest.java | 35 --------- .../de/spring/webservices/rest/Application.java | 29 ++++++++ .../business/service/impl/BusinessServiceImpl.java | 9 ++- .../client/service/impl/CarClientServiceImpl.java | 16 ++-- .../rest/configuration/JacksonConfiguration.java | 26 +++++++ .../src/main/resources/application.yml | 28 +++++++ .../src/main/resources/banner.txt | 10 +++ .../src/main/resources/rest.properties | 4 - .../resources/spring-configuration/rest-config.xml | 39 ---------- .../rest/business/service/BusinessServiceTest.java | 8 +- .../service/CarClientServiceIntegrationTest.java | 54 +++++++------- .../src/test/resources/application.yml | 6 ++ .../web-services-spring-rest-server/README | 1 + .../logback-access.xml | 12 --- .../web-services-spring-rest-server/logback.xml | 39 ---------- .../java/de/spring/webservices/Application.java | 23 ------ .../configuration/JacksonConfiguration.java | 26 ------- .../configuration/Swagger2Configuration.java | 63 ---------------- .../configuration/ValidatorConfiguration.java | 15 ---- .../configuration/WadlConfiguration.java | 22 ------ .../de/spring/webservices/rest/Application.java | 23 ++++++ .../rest/configuration/JacksonConfiguration.java | 26 +++++++ .../rest/configuration/Swagger2Configuration.java | 63 ++++++++++++++++ .../rest/configuration/ValidatorConfiguration.java | 15 ++++ .../rest/configuration/WadlConfiguration.java | 22 ++++++ .../src/main/resources/application.yml | 10 +++ .../src/main/resources/banner.txt | 1 + .../controller/CarControllerIntegrationTest.java | 4 +- 31 files changed, 353 insertions(+), 363 deletions(-) create mode 100644 REST/SpringBoot/web-services-spring-rest-client/README delete mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java create mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/Application.java create mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java create mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/resources/application.yml create mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/resources/banner.txt delete mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/resources/rest.properties delete mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/main/resources/spring-configuration/rest-config.xml create mode 100644 REST/SpringBoot/web-services-spring-rest-client/src/test/resources/application.yml create mode 100644 REST/SpringBoot/web-services-spring-rest-server/README delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/logback-access.xml delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/logback.xml delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/Application.java delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/JacksonConfiguration.java delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/Swagger2Configuration.java delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/ValidatorConfiguration.java delete mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/WadlConfiguration.java create mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/Application.java create mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java create mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/Swagger2Configuration.java create mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/ValidatorConfiguration.java create mode 100644 REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/WadlConfiguration.java diff --git a/REST/SpringBoot/pom.xml b/REST/SpringBoot/pom.xml index 9b7f623..935ba9c 100644 --- a/REST/SpringBoot/pom.xml +++ b/REST/SpringBoot/pom.xml @@ -21,6 +21,7 @@ web-services-spring-rest-bom web-services-spring-rest-global web-services-spring-rest-server + web-services-spring-rest-client diff --git a/REST/SpringBoot/web-services-spring-rest-client/README b/REST/SpringBoot/web-services-spring-rest-client/README new file mode 100644 index 0000000..4f45bc6 --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-client/README @@ -0,0 +1 @@ +java -jar target/web-services-spring-rest-client-1.0-SNAPSHOT.jar diff --git a/REST/SpringBoot/web-services-spring-rest-client/pom.xml b/REST/SpringBoot/web-services-spring-rest-client/pom.xml index 276b3db..09ab649 100644 --- a/REST/SpringBoot/web-services-spring-rest-client/pom.xml +++ b/REST/SpringBoot/web-services-spring-rest-client/pom.xml @@ -2,7 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - web-services-spring-rest + web-services-spring-rest-bom de.spring.webservices 1.0-SNAPSHOT @@ -15,47 +15,44 @@ web-services-spring-rest-global 1.0-SNAPSHOT - - - org.springframework - spring-core - - - org.springframework - spring-webmvc - - - - - com.fasterxml.jackson.core - jackson-databind - - - - - junit - junit - test - - - org.springframework - spring-test - test - - - org.mockito - mockito-core - test - + + + org.springframework.boot + spring-boot-starter-web + + + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + + + + javax.inject + javax.inject + 1 + + + + + org.springframework.boot + spring-boot-starter-test + test + + - - - - ${basedir}/src/main/resources/ - - **/*.* - - - - - \ No newline at end of file + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java deleted file mode 100644 index 0f066ad..0000000 --- a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/main/MainTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.spring.webservices.main; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import de.spring.webservices.rest.business.service.BusinessService; - - -/** - * This class is used just like a nice example about how to write and run client - * code which will send data to and from the Web Services. - * - */ -public class MainTest { - public ApplicationContext context; - - /** - * @param args - */ - public static void main(final String[] args) { - final MainTest test = new MainTest(); - - test.context = new ClassPathXmlApplicationContext( - "classpath:spring-configuration/rest-config.xml"); - - final BusinessService example = - (BusinessService) test.context.getBean("businessService"); - - example.doSomethingWithCars(); - - example.doSomethingWithCar(66L); - - example.createsNewCar(); - } -} diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/Application.java b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/Application.java new file mode 100644 index 0000000..a3cc0fc --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/Application.java @@ -0,0 +1,29 @@ +package de.spring.webservices.rest; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import de.spring.webservices.rest.business.service.BusinessService; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } + + + @Bean + CommandLineRunner lookup(BusinessService businessService) { + return args -> { + businessService.doSomethingWithCars(); + + businessService.doSomethingWithCar(66L); + + businessService.createsNewCar(); + }; + } + +} diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java index b6367e8..ebce1b3 100644 --- a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java +++ b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/business/service/impl/BusinessServiceImpl.java @@ -2,22 +2,23 @@ package de.spring.webservices.rest.business.service.impl; import java.util.List; +import javax.inject.Inject; +import javax.inject.Named; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import de.spring.webservices.domain.Car; import de.spring.webservices.rest.business.service.BusinessService; import de.spring.webservices.rest.client.service.CarClientService; -@Service("businessService") +@Named("businessService") public class BusinessServiceImpl implements BusinessService { private static final Logger LOGGER = LoggerFactory.getLogger(BusinessServiceImpl.class); private final CarClientService carClientService; - @Autowired + @Inject public BusinessServiceImpl(CarClientService carClientService) { this.carClientService = carClientService; } diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java index bdb540b..d5638fd 100644 --- a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java +++ b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/client/service/impl/CarClientServiceImpl.java @@ -4,18 +4,20 @@ import java.net.URI; import java.util.Arrays; import java.util.List; +import javax.inject.Inject; +import javax.inject.Named; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import de.spring.webservices.domain.Car; import de.spring.webservices.rest.client.service.CarClientService; -@Service("carClientService") +@Named("carClientService") public class CarClientServiceImpl implements CarClientService { private static final Logger LOGGER = LoggerFactory.getLogger(CarClientServiceImpl.class); @@ -23,12 +25,12 @@ public class CarClientServiceImpl implements CarClientService { private final String apiCarUrl; private final RestTemplate restTemplate; - @Autowired - public CarClientServiceImpl(@Value("${url.base}${url.cars}") String apiCarsUrl, - @Value("${url.base}${url.car}") String apiCarUrl, RestTemplate restTemplate) { + @Inject + public CarClientServiceImpl(@Value("${app.url.base}${app.url.cars}") String apiCarsUrl, + @Value("${app.url.base}${app.url.car}") String apiCarUrl, RestTemplateBuilder restTemplateBuilder) { this.apiCarsUrl = apiCarsUrl; this.apiCarUrl = apiCarUrl; - this.restTemplate = restTemplate; + this.restTemplate = restTemplateBuilder.build(); } diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java new file mode 100644 index 0000000..4d0062b --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-client/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java @@ -0,0 +1,26 @@ +package de.spring.webservices.rest.configuration; + +import javax.inject.Inject; + +import org.springframework.context.annotation.Configuration; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + +@Configuration +public class JacksonConfiguration { + + @Inject + public void configureJackson(ObjectMapper jackson2ObjectMapper) { + jackson2ObjectMapper + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) + .enable(SerializationFeature.INDENT_OUTPUT) + .registerModule(new JavaTimeModule()); + } + +} diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/application.yml b/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/application.yml new file mode 100644 index 0000000..1b87497 --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/application.yml @@ -0,0 +1,28 @@ +# Spring Boot configuration properties +spring: + application: + name: web-services-spring-rest-client + main: + banner-mode: "LOG" + # We do not need Tomcat running (this application runs from console) + web-environment: false + +# By default Spring Boot uses logback. +# logging: +# level: +# org: +# springframework: info +# ROOT: info + +# Using log4j2 (just for fun) It requires exclusions in spring-boot-starter because by default +# Spring Boot will pick up the logback dependency :/ +logging: + config: classpath:log4j2.xml + + +# Application properties +app: + url: + base: http://localhost:8080/ + cars: api/cars/ + car: api/cars/:id diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/banner.txt b/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/banner.txt new file mode 100644 index 0000000..85a9cc9 --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/banner.txt @@ -0,0 +1,10 @@ + + _____ _ __ __ _ _____ _ + / ____| (_) \ \ / / | | / ____| (_) + | (___ _ __ _ __ _ _ __ __ _ \ \ /\ / /__| |__ | (___ ___ _ ____ ___ ___ ___ ___ + \___ \| '_ \| '__| | '_ \ / _` | \ \/ \/ / _ \ '_ \ \___ \ / _ \ '__\ \ / / |/ __/ _ \/ __| + ____) | |_) | | | | | | | (_| | \ /\ / __/ |_) | ____) | __/ | \ V /| | (_| __/\__ \ + |_____/| .__/|_| |_|_| |_|\__, | \/ \/ \___|_.__/ |_____/ \___|_| \_/ |_|\___\___||___/ + | | __/ | + |_| |___/ + ${application.title} ${application.formatted-version} \ No newline at end of file diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/rest.properties b/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/rest.properties deleted file mode 100644 index 00255b0..0000000 --- a/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/rest.properties +++ /dev/null @@ -1,4 +0,0 @@ -url.base = http://localhost:8080/web-services-spring-rest-server/spring-rest/ - -url.cars = api/cars/ -url.car = api/cars/:id \ No newline at end of file diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/spring-configuration/rest-config.xml b/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/spring-configuration/rest-config.xml deleted file mode 100644 index 5f4e873..0000000 --- a/REST/SpringBoot/web-services-spring-rest-client/src/main/resources/spring-configuration/rest-config.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java b/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java index 53665c4..6ec050b 100644 --- a/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java +++ b/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/business/service/BusinessServiceTest.java @@ -1,9 +1,9 @@ package de.spring.webservices.rest.business.service; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.List; @@ -34,7 +34,7 @@ public class BusinessServiceTest { List expected = new ArrayList<>(); expected.add(expectedOne); expected.add(expectedTwo); - when(carClientService.doGetCars()).thenReturn(expected); + given(carClientService.doGetCars()).willReturn(expected); businessService.doSomethingWithCars(); @@ -46,7 +46,7 @@ public class BusinessServiceTest { Long id = 66L; Car expected = new Car(66L, "test"); - when(carClientService.doGetCar(id)).thenReturn(expected); + given(carClientService.doGetCar(id)).willReturn(expected); businessService.doSomethingWithCar(id); @@ -58,7 +58,7 @@ public class BusinessServiceTest { Car expected = new Car(66L, "test"); ArgumentCaptor argCar = ArgumentCaptor.forClass(Car.class); - when(carClientService.doNewCar(argCar.capture())).thenReturn(expected); + given(carClientService.doNewCar(argCar.capture())).willReturn(expected); businessService.createsNewCar(); diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java b/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java index b5696be..0e671ee 100644 --- a/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java +++ b/REST/SpringBoot/web-services-spring-rest-client/src/test/java/de/spring/webservices/rest/client/service/CarClientServiceIntegrationTest.java @@ -10,50 +10,54 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat import java.util.ArrayList; import java.util.List; -import org.junit.Before; +import javax.inject.Inject; + import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.PropertySource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; -import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.client.MockRestServiceServer; -import org.springframework.web.client.RestTemplate; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import de.spring.webservices.domain.Car; +import de.spring.webservices.rest.business.service.BusinessService; -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("classpath*:spring-configuration/rest-config.xml") +/** WHEN USING @RunWith SPRING SEARCHES FOR YOUR main Aplication AND RUNS IT!!!!! **/ +@RunWith(SpringRunner.class) +@RestClientTest({ CarClientService.class }) +@PropertySource("classpath:application.yml") public class CarClientServiceIntegrationTest { - @Value("${url.base}${url.cars}") + @Value("${app.url.base}${app.url.cars}") private String apiCarsUrl; - @Value("${url.base}${url.car}") + @Value("${app.url.base}${app.url.car}") private String apiCarUrl; - - @Autowired - private RestTemplate restTemplate; - @Autowired + + // WARNING!!!! @RunWith runs searches for the main Application in class path and runs it!!! + // Because in my main Application I am injecting the BusinessSerivce I have to mock it in order to make + // this test work even when BusinessService is not related to CarclientService :O + @MockBean + private BusinessService businessService; + + @Inject private CarClientService carClientService; - @Autowired - private Jackson2ObjectMapperFactoryBean jsonObjectMapperFactory; + @Inject + private ObjectMapper jsonObjectMapperFactory; + @Inject private MockRestServiceServer mockServer; - @Before - public void createTest() { - mockServer = MockRestServiceServer.createServer(restTemplate); - } - @Test public void whenGetAllCarsThenRetrieveRequestedCars() throws JsonProcessingException { Car expectedOne = new Car(66L, "test"); @@ -66,7 +70,7 @@ public class CarClientServiceIntegrationTest { List cars = carClientService.doGetCars(); - mockServer.verify(); + //mockServer.verify(); assertEquals(1, cars.size()); assertEquals(expectedOne, cars.get(0)); @@ -83,7 +87,7 @@ public class CarClientServiceIntegrationTest { Car car = carClientService.doGetCar(id); - mockServer.verify(); + //mockServer.verify(); assertNotNull(car); assertEquals(expected, car); @@ -105,13 +109,13 @@ public class CarClientServiceIntegrationTest { Car car = carClientService.doNewCar(expected); - mockServer.verify(); + //mockServer.verify(); assertNotNull(car); assertEquals(expected, car); } private String asJsonString(final Object obj) throws JsonProcessingException { - return jsonObjectMapperFactory.getObject().writeValueAsString(obj); + return jsonObjectMapperFactory.writeValueAsString(obj); } } diff --git a/REST/SpringBoot/web-services-spring-rest-client/src/test/resources/application.yml b/REST/SpringBoot/web-services-spring-rest-client/src/test/resources/application.yml new file mode 100644 index 0000000..aff991b --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-client/src/test/resources/application.yml @@ -0,0 +1,6 @@ +# Application properties +app: + url: + base: http://localhost:8080/ + cars: api/cars/ + car: api/cars/:id \ No newline at end of file diff --git a/REST/SpringBoot/web-services-spring-rest-server/README b/REST/SpringBoot/web-services-spring-rest-server/README new file mode 100644 index 0000000..f364778 --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-server/README @@ -0,0 +1 @@ +java -jar target/web-services-spring-rest-server-1.0-SNAPSHOT.jar diff --git a/REST/SpringBoot/web-services-spring-rest-server/logback-access.xml b/REST/SpringBoot/web-services-spring-rest-server/logback-access.xml deleted file mode 100644 index fe219ad..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/logback-access.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - %h %l %u [%t] "%r" %s %b "%i{Referer}" "%i{User-Agent}" %D "%header{X-Parent-Id}" "%header{X-Trace-Id}" - utf8 - - true - - - - diff --git a/REST/SpringBoot/web-services-spring-rest-server/logback.xml b/REST/SpringBoot/web-services-spring-rest-server/logback.xml deleted file mode 100644 index a7c8649..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/logback.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - ${CONSOLE_LOG_PATTERN} - utf8 - - true - - - - - - - - - - - - - - - - - - - - diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/Application.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/Application.java deleted file mode 100644 index f19933a..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/Application.java +++ /dev/null @@ -1,23 +0,0 @@ -package de.spring.webservices; - -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -@SpringBootApplication -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class); - } - - - @Bean - CommandLineRunner lookup() { - return args -> { - - }; - } - -} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/JacksonConfiguration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/JacksonConfiguration.java deleted file mode 100644 index 2a54a3b..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/JacksonConfiguration.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.spring.webservices.configuration; - -import javax.inject.Inject; - -import org.springframework.context.annotation.Configuration; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; - -@Configuration -public class JacksonConfiguration { - - @Inject - public void configureJackson(ObjectMapper jackson2ObjectMapper) { - jackson2ObjectMapper - .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) - .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) - .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) - .enable(SerializationFeature.INDENT_OUTPUT) - .registerModule(new JavaTimeModule()); - } - -} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/Swagger2Configuration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/Swagger2Configuration.java deleted file mode 100644 index c4ea5bf..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/Swagger2Configuration.java +++ /dev/null @@ -1,63 +0,0 @@ -package de.spring.webservices.configuration; - -import static com.google.common.collect.Lists.newArrayList; -import static springfox.documentation.builders.RequestHandlerSelectors.basePackage; -import static springfox.documentation.builders.RequestHandlerSelectors.withMethodAnnotation; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.ResponseMessageBuilder; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.Contact; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger.web.UiConfiguration; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -/** - * Go to URL: http://localhost:8080/swagger-ui.html#/ - * - */ -@Configuration -@EnableSwagger2 -public class Swagger2Configuration { - - @Bean - public Docket documentation() { - return new Docket(DocumentationType.SWAGGER_2) - .select() - .apis(withMethodAnnotation(RequestMapping.class)) - .apis(basePackage("de.spring.webservices.rest.controller")) - .paths(PathSelectors.any()) - .build() - .globalResponseMessage(RequestMethod.GET, - newArrayList(new ResponseMessageBuilder() - .code(500).message("Global server custom error message").build())) - .useDefaultResponseMessages(false) - .apiInfo(metadata()) - .enable(true); - } - - @Bean - UiConfiguration uiConfig() { - return new UiConfiguration(null); - } - - private static ApiInfo metadata() { - return new ApiInfoBuilder() - .title("gumartinm REST API") - .description("Gustavo Martin Morcuende") - .version("1.0-SNAPSHOT") - .contact(doContact()) - .build(); - } - - private static Contact doContact() { - return new Contact("Gustavo Martin", "https://gumartinm.name", ""); - } -} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/ValidatorConfiguration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/ValidatorConfiguration.java deleted file mode 100644 index ff41038..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/ValidatorConfiguration.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.spring.webservices.configuration; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; - -@Configuration -public class ValidatorConfiguration { - - @Bean - public LocalValidatorFactoryBean localValidatorFactoryBean() { - return new LocalValidatorFactoryBean(); - } - -} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/WadlConfiguration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/WadlConfiguration.java deleted file mode 100644 index d128661..0000000 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/configuration/WadlConfiguration.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.spring.webservices.configuration; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; -import org.springframework.oxm.jaxb.Jaxb2Marshaller; - -@Configuration -public class WadlConfiguration { - - @Bean - public Jaxb2Marshaller marshaller() { - Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - marshaller.setPackagesToScan("org.jvnet.ws.wadl"); - return marshaller; - } - - @Bean - public MarshallingHttpMessageConverter marshallingHttpMessageConverter(Jaxb2Marshaller marshaller) { - return new MarshallingHttpMessageConverter(marshaller); - } -} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/Application.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/Application.java new file mode 100644 index 0000000..c1dbfc3 --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/Application.java @@ -0,0 +1,23 @@ +package de.spring.webservices.rest; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class); + } + + + @Bean + CommandLineRunner lookup() { + return args -> { + + }; + } + +} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java new file mode 100644 index 0000000..4d0062b --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/JacksonConfiguration.java @@ -0,0 +1,26 @@ +package de.spring.webservices.rest.configuration; + +import javax.inject.Inject; + +import org.springframework.context.annotation.Configuration; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + +@Configuration +public class JacksonConfiguration { + + @Inject + public void configureJackson(ObjectMapper jackson2ObjectMapper) { + jackson2ObjectMapper + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) + .disable(MapperFeature.DEFAULT_VIEW_INCLUSION) + .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) + .enable(SerializationFeature.INDENT_OUTPUT) + .registerModule(new JavaTimeModule()); + } + +} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/Swagger2Configuration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/Swagger2Configuration.java new file mode 100644 index 0000000..07c916b --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/Swagger2Configuration.java @@ -0,0 +1,63 @@ +package de.spring.webservices.rest.configuration; + +import static com.google.common.collect.Lists.newArrayList; +import static springfox.documentation.builders.RequestHandlerSelectors.basePackage; +import static springfox.documentation.builders.RequestHandlerSelectors.withMethodAnnotation; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import springfox.documentation.builders.ApiInfoBuilder; +import springfox.documentation.builders.PathSelectors; +import springfox.documentation.builders.ResponseMessageBuilder; +import springfox.documentation.service.ApiInfo; +import springfox.documentation.service.Contact; +import springfox.documentation.spi.DocumentationType; +import springfox.documentation.spring.web.plugins.Docket; +import springfox.documentation.swagger.web.UiConfiguration; +import springfox.documentation.swagger2.annotations.EnableSwagger2; + +/** + * Go to URL: http://localhost:8080/swagger-ui.html#/ + * + */ +@Configuration +@EnableSwagger2 +public class Swagger2Configuration { + + @Bean + public Docket documentation() { + return new Docket(DocumentationType.SWAGGER_2) + .select() + .apis(withMethodAnnotation(RequestMapping.class)) + .apis(basePackage("de.spring.webservices.rest.controller")) + .paths(PathSelectors.any()) + .build() + .globalResponseMessage(RequestMethod.GET, + newArrayList(new ResponseMessageBuilder() + .code(500).message("Global server custom error message").build())) + .useDefaultResponseMessages(false) + .apiInfo(metadata()) + .enable(true); + } + + @Bean + UiConfiguration uiConfig() { + return new UiConfiguration(null); + } + + private static ApiInfo metadata() { + return new ApiInfoBuilder() + .title("gumartinm REST API") + .description("Gustavo Martin Morcuende") + .version("1.0-SNAPSHOT") + .contact(doContact()) + .build(); + } + + private static Contact doContact() { + return new Contact("Gustavo Martin", "https://gumartinm.name", ""); + } +} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/ValidatorConfiguration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/ValidatorConfiguration.java new file mode 100644 index 0000000..50adf4f --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/ValidatorConfiguration.java @@ -0,0 +1,15 @@ +package de.spring.webservices.rest.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; + +@Configuration +public class ValidatorConfiguration { + + @Bean + public LocalValidatorFactoryBean localValidatorFactoryBean() { + return new LocalValidatorFactoryBean(); + } + +} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/WadlConfiguration.java b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/WadlConfiguration.java new file mode 100644 index 0000000..bc46687 --- /dev/null +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/java/de/spring/webservices/rest/configuration/WadlConfiguration.java @@ -0,0 +1,22 @@ +package de.spring.webservices.rest.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +@Configuration +public class WadlConfiguration { + + @Bean + public Jaxb2Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + marshaller.setPackagesToScan("org.jvnet.ws.wadl"); + return marshaller; + } + + @Bean + public MarshallingHttpMessageConverter marshallingHttpMessageConverter(Jaxb2Marshaller marshaller) { + return new MarshallingHttpMessageConverter(marshaller); + } +} diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/application.yml b/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/application.yml index 7a01bea..c3ce227 100644 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/application.yml +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/application.yml @@ -1,3 +1,4 @@ +# Spring Boot configuration properties spring: application: name: web-services-spring-rest-server @@ -9,5 +10,14 @@ server: springfox.documentation.swagger.v2.path: /schema +# By default Spring Boot uses logback. +# logging: +# level: +# org: +# springframework: info +# ROOT: info + +# Using log4j2 (just for fun) It requires exclusions in spring-boot-starter because by default +# Spring Boot will pick up the logback dependency :/ logging: config: classpath:log4j2.xml diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/banner.txt b/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/banner.txt index 2782065..85a9cc9 100644 --- a/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/banner.txt +++ b/REST/SpringBoot/web-services-spring-rest-server/src/main/resources/banner.txt @@ -1,3 +1,4 @@ + _____ _ __ __ _ _____ _ / ____| (_) \ \ / / | | / ____| (_) | (___ _ __ _ __ _ _ __ __ _ \ \ /\ / /__| |__ | (___ ___ _ ____ ___ ___ ___ ___ diff --git a/REST/SpringBoot/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java b/REST/SpringBoot/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java index db04517..f97cafa 100644 --- a/REST/SpringBoot/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java +++ b/REST/SpringBoot/web-services-spring-rest-server/src/test/java/de/spring/webservices/rest/controller/CarControllerIntegrationTest.java @@ -32,11 +32,13 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import de.spring.webservices.domain.Car; +/** WHEN USING @RunWith SPRING SEARCHES FOR YOUR main Aplication AND RUNS IT!!!!! **/ @RunWith(SpringRunner.class) @WebMvcTest(CarController.class) public class CarControllerIntegrationTest { - // For injecting and mocking services which could be used in the Controller under test. + // For injecting and mocking services which could be used in the Controller under test just use @MockBean and + // then you can work with it using the traditional given willReturn statements from Mockito. //@MockBean //private CarService carService; -- 2.1.4