Skip to content

Instantly share code, notes, and snippets.

@bigalnz
Created December 11, 2019 07:28
Show Gist options
  • Save bigalnz/650a5bccdb907e315d108e922e8933d4 to your computer and use it in GitHub Desktop.
Save bigalnz/650a5bccdb907e315d108e922e8933d4 to your computer and use it in GitHub Desktop.
package com.example.depdev.controller;
import com.example.depdev.entity.Task;
import com.example.depdev.repository.TaskRepository;
import com.example.depdev.service.TaskService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
@Component
@Controller
@RequestMapping("/")
public class TestController {
private TaskRepository taskRepository;
@Autowired
TaskService taskService;
@Autowired
public void setTaskRepository(TaskRepository taskRepository) {this.taskRepository = taskRepository;}
@GetMapping("/alltasks")
@ResponseBody
public List allTasks() throws JsonProcessingException {
GeometryFactory gf = new GeometryFactory();
Double y = -36.829;
Double x = 174.896;
Task testTask = new Task();
testTask.setId(new Long(01));
testTask.setTitle("Test Task");
Point p = gf.createPoint(new Coordinate(x, y));
p.setSRID(4326);
testTask.setLocation(p);
taskRepository.save(testTask);
ObjectMapper objectMapper = new ObjectMapper();
String geoJson = objectMapper.writeValueAsString(testTask);
List<Task> listTasks = new ArrayList<>();
listTasks = taskService.findAll();
return listTasks;
}
}
package com.example.depdev.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import com.bedatadriven.jackson.datatype.jts.serialization.GeometryDeserializer;
import com.bedatadriven.jackson.datatype.jts.serialization.GeometrySerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.locationtech.jts.geom.Geometry;
@Entity
@Table(name = "task")
public class Task {
public Task () {
};
@Id
private Long id;
private String title;
@Column(columnDefinition = "geometry(Point,4326)")
@JsonSerialize(using = GeometrySerializer.class)
@JsonDeserialize(contentUsing = GeometryDeserializer.class)
private Geometry location;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment