From b075ca6358b375d5227b9e9ecb4a53aea8e65a2c Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 3 Apr 2016 17:37:58 +0200 Subject: [PATCH] #177 - Polished domain model. Inlined Address type into Store. Moved to @Value and removed code that got obsolete with that. Removed custom WebConfig that's not needed anymore with Spring Boot. --- .../springdata/rest/stores/Address.java | 43 ------------------ .../{StoreApp.java => Application.java} | 15 ++----- .../example/springdata/rest/stores/Store.java | 44 ++++++++++++------- .../rest/stores/StoreInitializer.java | 7 +-- .../springdata/rest/stores/WebConfig.java | 43 ------------------ .../StoreRepositoryIntegrationTests.java | 9 ++-- 6 files changed, 38 insertions(+), 123 deletions(-) delete mode 100644 rest/starbucks/src/main/java/example/springdata/rest/stores/Address.java rename rest/starbucks/src/main/java/example/springdata/rest/stores/{StoreApp.java => Application.java} (66%) delete mode 100644 rest/starbucks/src/main/java/example/springdata/rest/stores/WebConfig.java diff --git a/rest/starbucks/src/main/java/example/springdata/rest/stores/Address.java b/rest/starbucks/src/main/java/example/springdata/rest/stores/Address.java deleted file mode 100644 index ccd7e018..00000000 --- a/rest/starbucks/src/main/java/example/springdata/rest/stores/Address.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2014-2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.rest.stores; - -import static org.springframework.data.mongodb.core.index.GeoSpatialIndexType.*; - -import lombok.Value; - -import org.springframework.data.geo.Point; -import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; - -/** - * Value object to represent an {@link Address}. - * - * @author Oliver Gierke - */ -@Value -public class Address { - - private final String street, city, zip; - private final @GeoSpatialIndexed(type = GEO_2DSPHERE) Point location; - - /* - * (non-Javadoc) - * @see java.lang.Object#toString() - */ - public String toString() { - return String.format("%s, %s %s", street, zip, city); - } -} diff --git a/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreApp.java b/rest/starbucks/src/main/java/example/springdata/rest/stores/Application.java similarity index 66% rename from rest/starbucks/src/main/java/example/springdata/rest/stores/StoreApp.java rename to rest/starbucks/src/main/java/example/springdata/rest/stores/Application.java index eebb66da..3b4471c6 100644 --- a/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreApp.java +++ b/rest/starbucks/src/main/java/example/springdata/rest/stores/Application.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,6 @@ package example.springdata.rest.stores; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; /** * Spring configuration class main application bootstrap point. @@ -28,14 +24,9 @@ import com.fasterxml.jackson.module.paramnames.ParameterNamesModule; * @author Oliver Gierke */ @SpringBootApplication -public class StoreApp { +public class Application { public static void main(String[] args) { - SpringApplication.run(StoreApp.class, args); - } - - // Workaround for https://github.com/spring-projects/spring-boot/issues/4336 - public @Bean Module namesModule() { - return new ParameterNamesModule(); + SpringApplication.run(Application.class, args); } } diff --git a/rest/starbucks/src/main/java/example/springdata/rest/stores/Store.java b/rest/starbucks/src/main/java/example/springdata/rest/stores/Store.java index 650d38e8..e8860562 100644 --- a/rest/starbucks/src/main/java/example/springdata/rest/stores/Store.java +++ b/rest/starbucks/src/main/java/example/springdata/rest/stores/Store.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,9 +15,15 @@ */ package example.springdata.rest.stores; -import lombok.Data; +import static org.springframework.data.mongodb.core.index.GeoSpatialIndexType.*; + +import lombok.Value; + +import java.util.UUID; import org.springframework.data.annotation.Id; +import org.springframework.data.geo.Point; +import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; import org.springframework.data.mongodb.core.mapping.Document; /** @@ -25,25 +31,31 @@ import org.springframework.data.mongodb.core.mapping.Document; * * @author Oliver Gierke */ -@Data +@Value @Document public class Store { - private final @Id String id; - private final String name; - private final Address address; + @Id UUID id = UUID.randomUUID(); + String name; + Address address; - public Store(String name, Address address) { + /** + * Value object to represent an {@link Address}. + * + * @author Oliver Gierke + */ + @Value + public static class Address { - this.name = name; - this.address = address; - this.id = null; - } + String street, city, zip; + @GeoSpatialIndexed(type = GEO_2DSPHERE) Point location; - protected Store() { - - this.id = null; - this.name = null; - this.address = null; + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + public String toString() { + return String.format("%s, %s %s", street, zip, city); + } } } diff --git a/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreInitializer.java b/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreInitializer.java index d87c680c..f3f78ed3 100644 --- a/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreInitializer.java +++ b/rest/starbucks/src/main/java/example/springdata/rest/stores/StoreInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,6 +15,7 @@ */ package example.springdata.rest.stores; +import example.springdata.rest.stores.Store.Address; import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; @@ -80,8 +81,8 @@ public class StoreInitializer { lineMapper.setFieldSetMapper(fields -> { Point location = new Point(fields.readDouble("Longitude"), fields.readDouble("Latitude")); - Address address = new Address(fields.readString("Street Address"), fields.readString("City"), fields - .readString("Zip"), location); + Address address = new Address(fields.readString("Street Address"), fields.readString("City"), + fields.readString("Zip"), location); return new Store(fields.readString("Name"), address); }); diff --git a/rest/starbucks/src/main/java/example/springdata/rest/stores/WebConfig.java b/rest/starbucks/src/main/java/example/springdata/rest/stores/WebConfig.java deleted file mode 100644 index 892a3ab4..00000000 --- a/rest/starbucks/src/main/java/example/springdata/rest/stores/WebConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package example.springdata.rest.stores; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; - -/** - * Temporarily required manual configuration of resource handling to activate the resource chain handling. - * - * @author Brian Clozel - */ -@Configuration -public class WebConfig extends WebMvcConfigurerAdapter { - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/webjars/**").// - addResourceLocations("classpath:/META-INF/resources/webjars/").// - resourceChain(true); - } - - @Bean - public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { - return new ResourceUrlEncodingFilter(); - } -} diff --git a/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java b/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java index f1ed8609..c20e1343 100644 --- a/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java +++ b/rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,7 @@ package example.springdata.rest.stores; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import example.springdata.rest.stores.Address; -import example.springdata.rest.stores.Store; -import example.springdata.rest.stores.StoreApp; -import example.springdata.rest.stores.StoreRepository; +import example.springdata.rest.stores.Store.Address; import org.junit.After; import org.junit.Before; @@ -44,7 +41,7 @@ import org.springframework.test.context.web.WebAppConfiguration; */ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration -@SpringApplicationConfiguration(classes = StoreApp.class) +@SpringApplicationConfiguration(classes = Application.class) public class StoreRepositoryIntegrationTests { @Autowired StoreRepository repository;