I have a controller that looks like that
@RestController
public class LocationsController {
@Autowired
private EntityManager manager;
private String withinQuery =
"WITH L as\n" +
"\n" +
"(SELECT *\n" +
"FROM location\n" +
"\n" +
"WHERE ST_Distance(ST_FlipCoordinates(location.shape), ST_FlipCoordinates(ST_GeomFromGeoJSON('%s'\n" +
" )))=0)\n" +
"\n" +
"SELECT *\n" +
"FROM L\n" +
"WHERE id NOT IN (\n" +
"SELECT metalocation_id FROM location\n" +
"WHERE metalocation_id IS NOT NULL\n" +
")";
private String nearestQuery =
"select * from location order by ST_Distance(ST_FlipCoordinates(location.shape), ST_FlipCoordinates(St_GeomFromGeoJSON('%s'))) limit 1";
@RequestMapping(value="near", method = RequestMethod.GET)
public List<Location> getNearestLocations(@RequestParam(value = "point") String pointAsString) throws IOException {
List<Location> locationCloseToPoint = manager.createNativeQuery(String.format(withinQuery, pointAsString), Location.class).getResultList();
if (locationCloseToPoint.size() == 0) {
List<Location> closesLocation = manager.createNativeQuery(String.format(nearestQuery, pointAsString), Location.class)
.getResultList();
locationCloseToPoint.addAll(closesLocation);
}
return locationCloseToPoint;
}
}
As you can see it return list of locations.
@Entity
public class Location {
public Geometry getShape() {
return shape;
}
public void setShape(Geometry shape) {
this.shape = shape;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private Geometry shape;
@ManyToOne(cascade = CascadeType.ALL)
private Location metalocation;
The problem with that is I want to return this list in format that spring data rest uses for location resource with all hateoas fields and stuff. More specifically I want to have a link to metalocation in the output.
I've read about spring-hateoas and ResourceAssembler and @RepositoryRestController and I think I could replicate what spring-data-rest is doing via writing custom ResourceAssembler, but I don't want to, because you know, why would I want to write the code that is already written by spring-data-rest, right?
They doing all this assembling stuff automatically, right? Because I see it in the http output. So I think there should be a way to use it.