During a POST call to a mvc springboot application want to force a boolean parameter to fault

Having a MVC pattern Sprinboot application we want the user to send a POST call to register a new PuntoDiInteresse document into a MongoDB collection. The verificato field is entitled to show if the point has been validated by a Curatore so when we create a new one that field must be on false until the Curatore proceed to validate it.

Model:

package it.unicam.cs.LocalDevelopmentPlatform.luoghi;

import jakarta.persistence.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Objects;

@Document(collection = "PuntoDiInteresse")
public class PuntoDiInteresse {
    @Id
    private int _id;

    private Coordinata coordinata;
    private String nome;
    private TipologiaPunto tipologia;
    private boolean verificato;
    private boolean segnalato;
    private String descrizione;
    private String motivoSegnalazione;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PuntoDiInteresse that)) return false;
        return verificato == that.verificato && segnalato == that.segnalato && Objects.equals(coordinata, that.coordinata)
                && Objects.equals(nome, that.nome) && tipologia == that.tipologia && Objects.equals(descrizione, that.descrizione)
                && Objects.equals(motivoSegnalazione, that.motivoSegnalazione);
    }

    public PuntoDiInteresse(Coordinata coordinata, String nome, TipologiaPunto tipologia, String descrizione) {
        //TODO bisogna vedere come mai il deserializzatore bypassi totalmente questo costruttore quando va a creare un'istanza
        // e quindi si può fare injection e forzare il campo a true del verificato e del segnalato.
        this.coordinata = coordinata;
        this.nome = nome;
        this.tipologia = tipologia;
        this.descrizione = descrizione;
        this.setMotivoSegnalazione(null);
        this.setVerificato(false);
        this.setSegnalato(false);
        this._id = this.hashCode();
    }

    @Override
    public int hashCode() {
        return Math.abs(Objects.hash(coordinata, nome, tipologia, descrizione));
    }

    public int get_id() {
        return _id;
    }

    public void setSegnalato(boolean segnalato) {
        this.segnalato = segnalato;
    }

    public void setVerificato(boolean verificato) {
        this.verificato = verificato;
    }

    public Coordinata getCoordinata() {
        return coordinata;
    }

    public String getNome() {
        return nome;
    }

    public TipologiaPunto getTipologia() {
        return tipologia;
    }

    public boolean isVerificato() {return verificato; }

    public void verifica(){ this.verificato = true; }

    public boolean isSegnalato() {return segnalato; }

    public void segnala(){ this.segnalato= true; }

    public String getDescrizione() {
        return descrizione;
    }

    public String getMotivoSegnalazione() {
        return motivoSegnalazione;
    }

    public void setMotivoSegnalazione(String motivoSegnalazione) {
        this.motivoSegnalazione = motivoSegnalazione;
    }
}

Repository:

package it.unicam.cs.LocalDevelopmentPlatform.repository;

import it.unicam.cs.LocalDevelopmentPlatform.luoghi.PuntoDiInteresse;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PuntoDiInteresseRepo extends MongoRepository<PuntoDiInteresse, java.lang.Integer> {

    @Query("{ '_id' : ?0 }")
    PuntoDiInteresse findById(int id);

    @Query("{verificato : true}")
    @Override
    List<PuntoDiInteresse> findAll();

    @Query("{verificato : false}")
    List<PuntoDiInteresse> findAllFalse();

}

Controller:

package it.unicam.cs.LocalDevelopmentPlatform.controller;

import it.unicam.cs.LocalDevelopmentPlatform.luoghi.Itinerario;
import it.unicam.cs.LocalDevelopmentPlatform.service.PuntoInteresseService;
import org.springframework.web.bind.annotation.*;
import it.unicam.cs.LocalDevelopmentPlatform.luoghi.PuntoDiInteresse;

import it.unicam.cs.LocalDevelopmentPlatform.service.ItinerarioService;

@RestController
@RequestMapping("contributor")
public class ContributorController extends TuristaController {

    public ContributorController(PuntoInteresseService puntoInteresseService, ItinerarioService itinerarioService) {
        super(puntoInteresseService, itinerarioService);
    }

    @PostMapping(value = "/caricaPunto")
    public void caricaPunto(@RequestBody PuntoDiInteresse puntoDiInteresse) {
        puntoInteresseService.savePunto(puntoDiInteresse);
    }

    @PostMapping("/caricaItinerario")
    public void caricaItinerario(@RequestBody Itinerario itinerario){itinerarioService.saveItinerario(itinerario);}
}

Service:

package it.unicam.cs.LocalDevelopmentPlatform.service;

import it.unicam.cs.LocalDevelopmentPlatform.luoghi.PuntoDiInteresse;
import it.unicam.cs.LocalDevelopmentPlatform.repository.PuntoDiInteresseRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PuntoInteresseService {


    private final PuntoDiInteresseRepo puntoDiInteresseRepo;

    @Autowired
    public PuntoInteresseService(PuntoDiInteresseRepo puntoDiInteresseRepo) {
        this.puntoDiInteresseRepo = puntoDiInteresseRepo;
    }

    public List<PuntoDiInteresse> getAllPunti() {
        return puntoDiInteresseRepo.findAll();
    }

    public List<PuntoDiInteresse> getAllPuntiById(List<Integer> punti){return puntoDiInteresseRepo.findAllById(punti);}

    public PuntoDiInteresse getPuntoByID(int id) {
        return puntoDiInteresseRepo.findById(id);
    }

    public void savePunto(PuntoDiInteresse puntoDiInteresse) {
        puntoDiInteresseRepo.save(puntoDiInteresse);
    }

    public void deletePunto(int id){puntoDiInteresseRepo.deleteById(id);}

    public List<PuntoDiInteresse> getAllNotVerified() {
        return puntoDiInteresseRepo.findAllFalse();
    }

    public void verificaPunto(int id) {
        PuntoDiInteresse daVer = puntoDiInteresseRepo.findById(id);
        daVer.verifica();
        puntoDiInteresseRepo.deleteById(id);
        puntoDiInteresseRepo.save(daVer);
    }
}

POSTMAN request:

{
    "coordinata": {
        "latitudine": "{{randomLat}}",
        "longitudine": "{{randomLong}}"
    },
    "nome": "Name",
    "tipologia": "{{randomTipologia}}",
    "verificato": "true",    
    "descrizione": "Description"
}

We tried to put some constraint on the constructor using default parameter, using the set function and even rewrite a JSON deserializer but the program continues to let the user to create a new document forcing the field to true.

New contributor

MadLuke312 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật