我要输入3个参数,但我只会输入1个参数,其余参数是可选的。
我收到此错误:
“位置[2]处的参数为空。您需要调用ParameterRegistration#enablePassingNulls(true)才能传递空参数。”
我已经尝试过StoredProcedureQueryParameterRegistration,但是我仍然收到相同的消息,也许我不知道如何使用它。
服务
package com.app.service;
import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;
import org.springframework.stereotype.Service;
import com.app.dao.CrudUserDAO;
import com.app.model.ValidateApiResponse;
@Service
public class CrudUserService implements CrudUserDAO{
@PersistenceContext
private EntityManager em;
@SuppressWarnings({ "unchecked" })
@Override
public ValidateApiResponse listUsers(int op, String nom, String ema) {
ValidateApiResponse vapi = new ValidateApiResponse();
try {
StoredProcedureQuery sp = em.createStoredProcedureQuery("SP_CRUD_USERS");
sp.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
sp.registerStoredProcedureParameter(2, String.class, ParameterMode.IN);
sp.registerStoredProcedureParameter(3, String.class, ParameterMode.IN);
sp.setParameter(1, op);
sp.setParameter(2, nom);
sp.setParameter(3, ema);
//sp.executeUpdate();
vapi.dTable = sp.getResultList();
vapi.Attr = "OK";
//((StoredProcedureQueryParameterRegistration) sp.getParameter(2)).enablePassingNulls(true);
//((StoredProcedureQueryParameterRegistration) sp.getParameter(3)).enablePassingNulls(true);
} catch (Exception e) {
vapi.Error = e.getMessage();
}
return vapi;
}
}
控制器
package com.app.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.app.model.ApiResponse;
import com.app.model.ValidateApiResponse;
import com.app.service.CrudUserService;
@RestController
@RequestMapping(value = "/CrudUsers")
public class CrudUserController {
@Autowired
private CrudUserService service;
@RequestMapping(value = "/listAllUsers", method = RequestMethod.POST)
public ApiResponse CrudUsers(@RequestParam("opcion") int op,
@RequestParam(value = "nomUser", required = false) String nom,
@RequestParam(value = "emaUser", required = false) String ema){
ValidateApiResponse vapi = new ValidateApiResponse();
ApiResponse api = new ApiResponse();
try {
vapi = service.listUsers(op,nom,ema);
if (vapi.Error == null || vapi.Error == "") {
if (vapi.Attr == "OK") {
api.MsjCode = 200;
api.Msj = "OK";
api.dtCollection = vapi.dTable;
} else {
api.MsjCode = 400;
api.Msj = "Dismissed Request";
api.MsjDetail = vapi.Attr;
}
} else {
api.Msj = "Internal Server Error";
api.MsjDetail = vapi.Error;
api.MsjCode = 500;
}
} catch (Exception e) {
api.Msj = "Internal Server Error";
api.MsjDetail = e.getMessage();
api.MsjCode = 500;
}
return api;
}
}