/
boot-gorm-mongodb.groovy
72 lines (63 loc) · 2.21 KB
/
boot-gorm-mongodb.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@Grab('org.grails:gorm-mongodb-spring-boot:1.1.0.RELEASE')
import grails.persistence.*
import grails.mongodb.geo.*
import org.bson.types.ObjectId
import com.mongodb.BasicDBObject
import com.mongodb.Mongo
import org.springframework.http.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.mongodb.core.mapping.MongoMappingContext
import static org.springframework.web.bind.annotation.RequestMethod.*
@RestController
class CityController {
@RequestMapping(value="/", method = GET)
List index() {
City.list().collect { [name: it.name] }
}
@RequestMapping(value="/near/{cityName}", method = GET)
ResponseEntity near(@PathVariable String cityName) {
def city = City.where { name == cityName }.find()
if(city) {
List<City> closest = City.findAllByLocationNear(city.location)
return new ResponseEntity([name: closest[1].name], HttpStatus.OK)
}
else {
return new ResponseEntity(HttpStatus.NOT_FOUND)
}
}
@Autowired
Mongo mongo
@PostConstruct
void populateCities() {
City.withTransaction{
City.collection.remove(new BasicDBObject())
City.saveAll( [ new City(name:"London", location: Point.valueOf( [-0.125487, 51.508515] ) ),
new City(name:"Paris", location: Point.valueOf( [2.352222, 48.856614] ) ),
new City(name:"New York", location: Point.valueOf( [-74.005973, 40.714353] ) ),
new City(name:"San Francisco", location: Point.valueOf( [-122.419416, 37.774929] ) ) ] )
}
}
}
@Entity
class City {
ObjectId id
String name
Point location
static constraints = {
name blank:false
location nullable:false
}
static mapping = {
location geoIndex:'2dsphere'
}
}
/* boot 1.2:
Error creating bean with name 'mappingMongoConverter' defined in class path resource
[o/s/b/a/m/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 1 of type
[o.s.d.m.c.m.MongoMappingContext]
*/
@Bean
MongoMappingContext springDataMongoMappingContext() {
return new MongoMappingContext()
}