I have a method that retrieves data from the form, and passes them as parameters to a service method to perform the query, this method returns a json response, and in the view sends me a message if there is data or not data in the query.
Now what I want to do is get the result of that query, and if there is data, I can download a file with that information and if not, show the message, for this I have two methods in the controller, one in the answer json and the other one that has the functionality of downloading the file, which I passed the list that I got from the first method
@RequestMapping(value="view/obtieneInfo")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public JsonResponse obtieneInfor(@ResponseBody Factura fact, HttpServletRequest request, HttpServletResponse respons) throws ControlException{
JsonResponse response = new JsonResponse();
response.setExito(0);
response.setDatos(null);
try{
List<Factura> factList = obtieneInfoService.consultaDatos(id, cvePag, fecha);
if(factList !=null && !factList.isEmpty){
response.setExito(1);
response.setDatos(factList);
}else{
log.info("Esta vacio");
}
}catch(ControlException e){
throw new ControlException("Error", e);
}
return response;
}
@RequestMapping(value="view/obtieneArchivo")
public void obtieneArchivo(HttpServletRequest request, HttpServletResponse response, @RequestParam ("factList") String [] factList ) throws ControlException{
try{
if(factList !=null){
response.addHeader("Content-Type", "application/csv");
response.addHeader("Content-Disposition", "attachment; filename=archivo.csv");
PrintWriter out = response.getWriter();
out.write("columna1,columna2,columna3");
out.write("\n");
for(String factu: factList){
out.write(factu.toCSVRepresentation());
out.write("\n");
}
out.flush();
out.close();
}else{
log.info("Esta vacio");
}
}catch(ControlException e){
throw new ControlException("Error", e);
}
}
In my Invoice entity, I have this method:
public String toCSV(){
StringBuilder builder = new StringBuilder();
builder.append(getId()).append(“,”),
.
.
.
return builder.toString(),
}
and this is my view code, in which I have two functions, one is called obtienInf (), and what it does is to pass the json's response and validate whether there is data or not, and I have another function that it is called download (), which contains the url of my method 2 that performs the download of the file, and this function I send it to call inside obtainInf (), in the part where it validates that if there is data
function obtieneInf(){
if(consultar()){
if(!HelperForm.valido(consulta, validator)){
return;
}else{
var data = Helper.obtenerJsonForm(consulta);
var url="${pageContext.request.contextPath}/view/obtieneInfo";
$.ajax({
type:"POST",
contentType: "application/json",
url: url,
data: data,
dataType: 'json',
success: function(response){
if(response.exito==1){
HelperDialogo.mostrar("Si hay registros");
factList = response.datos;
descarga(factList);
}else{
HelperDialogo.mostrar("No se encontraron registros");
}
},
error: function(response){
HelperDialogo.mostrarError(response);
}
});
}
}
}
function descarga(factList){
var url="${pageContext.request.contextPath}/view/obtieneArchivo?factList=" +factList;
$.fileDownload(url)
.done(
function(){
Helper.muestraInf("Se descargo correctamente");
}
)
.fail(
function(){
Helper.muestraInf("Ocurrio un error en la descarga");
}
);
}
</script>
<form:form id="consulta" modelAttribute="consultaForm">
<tr>
<td><label>ID/label></td>
<td><form: input type="text" id="idPago" path="id"/></td>
</tr>
<tr>
<td><label>CLAVE PAGO</label></td>
<td><form: input type="text" id="clavePago" path="clavePago"/></td>
</tr>
<tr>
<tr>
<td><label>FECHA PAGO</label></td>
<td><form: input type="text" id="fechaPago" path="fechaPago"/></td>
<input type="button" onclick="obtieneInf()"></input>
</tr>
</form:form>
and when filling out my form and clicking on the button, it sends me a message saying that if there is data, that is what I have in my function and then sends me another message that an error occurred in the download, which is of the download method () and throws me the error:
Failed to convrt value of type java.lang.String to required type util.List; nested exception is java.lang.IllegalStateException can not convert value of type String to required type com.mx.Entity
What is wrong, my method of the controller to pass the list ?, or how I do to download the file? is my download method ok, when in the url I have "$ {pageContext.request.contextPath} / view / getFile? factList=" + factList;
I need your help, thank you.