'Failing to serialize geometry data in java rest api for postgis

Working on location based rest api and so far everything is working fine except for one controller with bring this error

2022-03-16 16:28:37.842 WARN 7720 --- [nio-8080-exec-5] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.Producttracking.dto.MetadataDto]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Conflicting setter definitions for property "x": org.postgis.Point#setX(double) vs org.postgis.Point#setX(int)

2022-03-16 16:28:37.847 WARN 7720 --- [nio-8080-exec-5] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.Producttracking.dto.MetadataDto]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Conflicting setter definitions for property "x": org.postgis.Point#setX(double) vs org.postgis.Point#setX(int)

here is the metadata entitty

package com.example.Producttracking.entity;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.n52.jackson.datatype.jts.GeometryDeserializer;
import org.n52.jackson.datatype.jts.GeometrySerializer;
import org.postgis.Point;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalTime;


@Entity
@Table
public class Metadata implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long meta_id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "emp_id", referencedColumnName = "emp_id")
    private Employee employee;
    private LocalDate date;
    private LocalTime time;

    @JsonSerialize(using = GeometrySerializer.class)
    @JsonDeserialize(contentUsing = GeometryDeserializer.class)
    private Point location;

    public Metadata() {
    }

    public Metadata(Long meta_id, Employee employee, LocalDate date, LocalTime time, Point location) {
        this.meta_id = meta_id;
        this.employee = employee;
        this.date = date;
        this.time = time;
        this.location = location;
    }

    public Metadata(Employee employee, LocalDate date, LocalTime time, Point location) {
        this.employee = employee;
        this.date = date;
        this.time = time;
        this.location = location;
    }

    public Long getMeta_id() {
        return meta_id;
    }

    public void setMeta_id(Long meta_id) {
        this.meta_id = meta_id;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public LocalTime getTime() {
        return time;
    }

    public void setTime(LocalTime time) {
        this.time = time;
    }

    public Point getLocation() {
        return location;
    }

    public void setLocation(Point location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Metadata{" +
                "meta_id=" + meta_id +
                ", employee=" + employee +
                ", date=" + date +
                ", time=" + time +
                ", location=" + location +
                '}';
    }
}

here is the the dto

package com.example.Producttracking.dto;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import org.n52.jackson.datatype.jts.GeometryDeserializer;
import org.n52.jackson.datatype.jts.GeometrySerializer;
import org.postgis.Point;
import java.time.LocalDate;
import java.time.LocalTime;

@Data
public class MetadataDto {
    private Long emp_id;
    private LocalDate date;
    private LocalTime time;

    @JsonSerialize(using = GeometrySerializer.class)
    @JsonDeserialize(contentUsing = GeometryDeserializer.class)
    private Point location;
}

here is the controller

package com.example.Producttracking.controller;

import com.example.Producttracking.dto.MetadataDto;
import com.example.Producttracking.entity.Metadata;
import com.example.Producttracking.services.MetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "api/v1/metadata")
public class MetadataController {

    private final MetadataService metadataService;

    @Autowired
    public MetadataController(MetadataService metadataService) {
        this.metadataService = metadataService;
    }
    
    @GetMapping
    public List<Metadata> getMetadata(){

        return metadataService.getMetadata();
    }

    @PostMapping
    public void registerMetadata (@RequestBody MetadataDto metadataDto){

        metadataService.registerMetadata(metadataDto);
    }
}

and here is the service

package com.example.Producttracking.services;

import com.example.Producttracking.dto.MetadataDto;
import com.example.Producttracking.entity.Metadata;
import com.example.Producttracking.repository.MetadataRepository;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MetadataService {

    private final MetadataRepository metadataRepository;
    private final ModelMapper modelMapper;

    @Autowired
    public MetadataService(MetadataRepository metadataRepository, ModelMapper modelMapper) {
        this.metadataRepository = metadataRepository;
        this.modelMapper = modelMapper;
    }

    public List<Metadata> getMetadata() {
        return metadataRepository.findAll();
    }

    public void registerMetadata(MetadataDto metadataDto) {

        Metadata metadata = modelMapper.map(metadataDto,Metadata.class);

        Optional<Metadata> metadataOptional = metadataRepository.findById(metadata.getMeta_id());

        if (metadataOptional.isPresent()){
            throw new IllegalStateException("metadata exist");
        }

        metadataRepository.save(metadata);
    }


}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source