#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.
This commit is contained in:
Oliver Gierke
2016-04-03 17:37:58 +02:00
parent 26a453af56
commit b075ca6358
6 changed files with 38 additions and 123 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
});

View File

@@ -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();
}
}

View File

@@ -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;