'spring boot jpa app, crudRepo Error (type=Bad Request, status=400). Validation failed for object

i´m working on a simple Spring-Boot CRUD app, where I´m trying to use the CrudRepository for updating or creating new entity instances and save them, but I keep getting the (type=Bad Request, status=400) error, but I don´t really have any validation, I don´t know were the error could be, I´m using old version of spring-boot for old Java compatibility because of the server I´ll be deploying the app. This is my Entity

@Entity
@Table(name = "AAA_TEST_DM_DATA")
public class DataDM implements Serializable{

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_TIENDA")
    private Tienda tienda;

    @Column(name = "NIVEL_NSE")
    private String nse;

    @Column(name = "GENERADOR_PRINCIPAL")
    private String generadorUno; 

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "DATA_ID")
    private Long id;

This is my Dao or repo where I extend the CrudRepository

public interface IDataDMDao extends CrudRepository <DataDM, Long> {

    DataDM findByTienda(Tienda tienda);
    
}

Here is my service

public interface IDataDMService {

    public List<DataDM> findAll();

    public DataDM findOne(Long id);

    public void save(DataDM dataDM);

    public boolean exists(Tienda tienda);

    public DataDM findDm(Tienda tienda);

    
}

Here is the service implementation

@Service
public class IDataDMServiceImp implements IDataDMService {

    @Autowired
    private IDataDMDao dataDMDao;


    @Override
    @Transactional(readOnly = true)
    public List<DataDM> findAll(){
        return (List<DataDM>) dataDMDao.findAll();
    }

    @Override
    @Transactional(readOnly = true)
    public DataDM findOne(Long id){
        return dataDMDao.findOne(id);
    }

    @Override
    @Transactional
    public void save(DataDM data){
        dataDMDao.save(data);
    }
    
    @Override
    @Transactional(readOnly = true)
    public boolean exists(Tienda tienda){
        
        if (dataDMDao.findByTienda(tienda) == null){
            return false;
        } else {
            return true;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public DataDM findDm(Tienda tienda){
        return dataDMDao.findByTienda(tienda);
    }
}

And here is the controller

@SessionAttributes("data")
public class DesController {

    @Autowired
    private ITiendaService tiendaService;

    @Autowired
    private IDataDMService dataService;

    @Autowired
    private ISesionTiendaService sesionService;


    //LOOK AT ALL DATADM
    @RequestMapping("/data")
    public String dataList(Model model){
        model.addAttribute("title", "Datos de tiendas");
        model.addAttribute("dataList", dataService.findAll());
        return "datas";

    }


    @RequestMapping("/captura")
    public String form(Model model, 
    @RequestParam(value = "_paramsP_ID") String idsesion,
    HttpServletRequest request){

        Integer idses = Integer.parseInt(request.getParameter("_paramsP_ID"));

        //get tiendaid based on idses
        SesionTienda sesion = sesionService.findSesion(idses, "FLT_TIENDA");
        Integer id = Integer.parseInt(sesion.getValor());

        //get tienda based on given id, then checks if there is data for that tienda
        Tienda tienda = tiendaService.findOne(id);
        boolean check = dataService.exists(tienda); 
        DataDM data = null;

         //if there is no data create new data entity for that tienda
         if (check == false){

            data = new DataDM();
            data.setTienda(tienda);
            //dataService.save(data);
            model.addAttribute("data", data);
            model.addAttribute("title", "Tiendas form");
            return "form";

        }

        //if there is data, find it and pass it into the model
        data = dataService.findDm(tienda);
        model.addAttribute("data", data);
        model.addAttribute("title", "Tiendas form");
        return "form";        
    }

    //Saves the DataDM entity from the form
    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String save(Model model, DataDM data, SessionStatus status)
    {
        dataService.save(data);
        status.setComplete();
        return "redirect:/success";
    } 

From a given parameter I get Tienda id, with that Id I want to see if the is a DataDM instance for that Tienda, if it exists update it with the form, if there is not an instance then create it and save it, everything works (findByTienda works) up until I click the save button from the form it gives me the error: (type=Bad Request, status=400). Validation failed for object='dataDM'. Error count: 1, but I don´t really have any validation to save the DataDM entity, I guess it has to be something with the save() method, but i have no idea what could be, can someone help me?

Edit: Adding the client code

    <!DOCTYPE html>
<html lang="en" xmlns:th="/http:wwww.thymeleaf.org">
<head th:replace="layout/layout :: head"></head>
<body>
<header th:replace="layout/layout :: header"></header>
    
    <div class="container">

        <h3 th:text=" 'CAPTURA PARA TIENDA ' + ${data.tienda.id} + ' ' + ${data.tienda.nombreTienda} "></h3>

        <div class="container">

            <form th:action="@{/form}" th:object="${data}" method="post" class="d-flex align-content-start flex-wrap p-2">

                <label for="sec-1" class="h4" th:text=" 'ALREDEDORES DE TIENDA' "></label>
                <div class="d-flex align-content-start flex-wrap" id="sec-1" name="sec-1" >
                    <div class="form-group p-2">
                        <select th:field="*{nse}" id='nse' name="nse" class="form-control">
                            <option th:value=" '-' "  th:text=" '-' "></option>
                            <option th:value=" 'AB' " th:text=" 'AB' "></option>
                            <option th:value=" 'C+' " th:text=" 'C+' "></option>
                            <option th:value=" 'C' "  th:text="  'C' "></option>
                            <option th:value=" 'C-' " th:text=" 'C-' "></option>
                            <option th:value=" 'D' "  th:text="  'D' "></option>
                        </select>
                    </div>
                <div class="form-group p-2">
                <input type="submit" value="Guardar Datos" class="btn btn-secondary btn-block" />
                </div>

                <input type="hidden" th:field="*{id}"/>
                <input type="hidden" th:field="*{tienda}"/>
                </form>
</div>



Solution 1:[1]

Bad Request means the server can't process the request (so your server code doesn't run), usually it means there is a client error, so you should check your client code...

EDIT: save method expects JSON by default, so you can either send JSON but it involves adding JavaScript or you can tell your API method to consume a different type of data, like this :

@PostMapping(
  path = "/save",
  consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})

Sources

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

Source: Stack Overflow

Solution Source
Solution 1