DATAREST-163 - Fixed rendering of embedded documents for MongoDB.

Removed the attempt to add a link to the response for non-associations. Switched to the usage of Simple(Property|Association)Handlers for simplified generics.
This commit is contained in:
Oliver Gierke
2013-11-14 09:48:37 +00:00
parent f965363650
commit 8f2e3eb4fe
7 changed files with 116 additions and 11 deletions

View File

@@ -2,7 +2,6 @@ package org.springframework.data.rest.core.domain.mongodb;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import com.mongodb.Mongo;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -11,17 +10,19 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.MongoClient;
/** /**
* @author Jon Brisbin * @author Jon Brisbin
*/ */
@Configuration @Configuration
@ComponentScan(basePackageClasses = { MongoDbRepositoryConfig.class }) @ComponentScan
@EnableMongoRepositories @EnableMongoRepositories
public class MongoDbRepositoryConfig { public class MongoDbRepositoryConfig {
@Bean @Bean
public MongoDbFactory mongoDbFactory() throws UnknownHostException { public MongoDbFactory mongoDbFactory() throws UnknownHostException {
return new SimpleMongoDbFactory(new Mongo("localhost"), "spring-data-rest"); return new SimpleMongoDbFactory(new MongoClient("localhost"), "spring-data-rest");
} }
@Bean @Bean

View File

@@ -276,9 +276,9 @@ public class PersistentEntityJackson2Module extends SimpleModule implements Init
return; return;
} }
if (property.isEntity() && maybeAddAssociationLink(builder, mappings, property, links)) { // if (property.isEntity() && maybeAddAssociationLink(builder, mappings, property, links)) {
return; // return;
} // }
// Property is a normal or non-managed property. // Property is a normal or non-managed property.
Object propertyValue = wrapper.getProperty(property); Object propertyValue = wrapper.getProperty(property);

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2013 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 org.springframework.data.rest.webmvc.mongodb;
/**
* @author Oliver Gierke
*/
public class Address {
public String street, zipCode;
}

View File

@@ -18,26 +18,24 @@ package org.springframework.data.rest.webmvc.mongodb;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.Mongo; import com.mongodb.MongoClient;
/** /**
* @author Jon Brisbin * @author Jon Brisbin
*/ */
@Configuration @Configuration
@ComponentScan
@EnableMongoRepositories @EnableMongoRepositories
public class MongoDbRepositoryConfig { public class MongoDbRepositoryConfig {
@Bean @Bean
public MongoDbFactory mongoDbFactory() throws UnknownHostException { public MongoDbFactory mongoDbFactory() throws UnknownHostException {
return new SimpleMongoDbFactory(new Mongo("localhost"), "spring-data-rest-example"); return new SimpleMongoDbFactory(new MongoClient("localhost"), "spring-data-rest-example");
} }
@Bean @Bean

View File

@@ -29,12 +29,15 @@ import org.springframework.hateoas.Link;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
/** /**
* Integration tests for MongoDB repositories.
*
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@ContextConfiguration(classes = MongoDbRepositoryConfig.class) @ContextConfiguration(classes = MongoDbRepositoryConfig.class)
public class MongoWebTests extends AbstractWebIntegrationTests { public class MongoWebTests extends AbstractWebIntegrationTests {
@Autowired ProfileRepository repository; @Autowired ProfileRepository repository;
@Autowired UserRepository userRepository;
@Before @Before
public void populateProfiles() { public void populateProfiles() {
@@ -48,11 +51,23 @@ public class MongoWebTests extends AbstractWebIntegrationTests {
linkedIn.setType("LinkedIn"); linkedIn.setType("LinkedIn");
repository.save(Arrays.asList(twitter, linkedIn)); repository.save(Arrays.asList(twitter, linkedIn));
Address address = new Address();
address.street = "Foo";
address.zipCode = "Bar";
User user = new User();
user.firstname = "Oliver";
user.lastname = "Gierke";
user.address = address;
userRepository.save(user);
} }
@After @After
public void cleanUp() { public void cleanUp() {
repository.deleteAll(); repository.deleteAll();
userRepository.deleteAll();
} }
/* /*
@@ -61,7 +76,7 @@ public class MongoWebTests extends AbstractWebIntegrationTests {
*/ */
@Override @Override
protected Iterable<String> expectedRootLinkRels() { protected Iterable<String> expectedRootLinkRels() {
return Arrays.asList("profiles"); return Arrays.asList("profiles", "users");
} }
@Test @Test
@@ -70,4 +85,12 @@ public class MongoWebTests extends AbstractWebIntegrationTests {
Link profileLink = discoverUnique("profiles"); Link profileLink = discoverUnique("profiles");
follow(profileLink).andExpect(jsonPath("$.content").value(hasSize(2))); follow(profileLink).andExpect(jsonPath("$.content").value(hasSize(2)));
} }
@Test
public void rendersEmbeddedDocuments() throws Exception {
Link usersLink = discoverUnique("users");
Link userLink = assertHasContentLinkWithRel("self", request(usersLink));
follow(userLink).andExpect(jsonPath("$.address.zipCode").value(is(notNullValue())));
}
} }

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2013 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 org.springframework.data.rest.webmvc.mongodb;
import java.math.BigInteger;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Oliver Gierke
*/
@Document
public class User {
public BigInteger id;
public String firstname, lastname;
public Address address;
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2013 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 org.springframework.data.rest.webmvc.mongodb;
import java.math.BigInteger;
import org.springframework.data.repository.CrudRepository;
/**
* @author Oliver Gierke
*/
public interface UserRepository extends CrudRepository<User, BigInteger> {
}