简体   繁体   English

spring 启动 jpa 应用程序,crudRepo 错误(类型=错误请求,状态=400)。 object 验证失败

[英]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.我正在开发一个简单的 Spring-Boot CRUD 应用程序,我正在尝试使用 CrudRepository 更新或创建新实体实例并保存它们,但我不断收到 (type=Bad Request, status=400) 错误,但我真的没有任何验证,我不知道错误可能是什么,我正在使用旧版本的 spring-boot 来实现旧的 Java 兼容性,因为我将部署应用程序的服务器。 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这是我扩展 CrudRepository 的 Dao 或 repo

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这是 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).从给定的参数中,我得到 Tienda id,使用该 ID,我想查看是否是该 Tienda 的 DataDM 实例,如果存在,则使用表单更新它,如果没有实例,则创建并保存它,一切正常(findByTienda 有效)直到我单击表单中的保存按钮,它给了我错误:(类型=错误请求,状态=400)。 Validation failed for object='dataDM'.对象 ='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?错误计数:1,但我真的没有任何验证来保存 DataDM 实体,我想它必须与 save() 方法有关,但我不知道可能是什么,有人可以帮助我吗?

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... Bad Request 意味着服务器无法处理请求(因此您的服务器代码无法运行),通常意味着存在客户端错误,因此您应该检查您的客户端代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM