public class ServletJsonToMap extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ParseException {
response.setContentType("application/pdf");
ServletOutputStream out = response.getOutputStream();
//deserializo el json
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try
{
JasperReport reporte = (JasperReport) JRLoader.loadObject(getServletContext().getRealPath("WEB-INF/prueba.jasper"));
HashMap parametros = new HashMap();
// mapeamos el json que se captura por URL
Server map = mapper.readValue(new URL("https://api.myjson.com/bins/2y29g"), Server.class);
// se recorre el objecto contacServer del Json
if(map != null){
for(ContactoServer t : map.getContactoServer()){
//System.out.println(t.getNmContacto() + "\n" + t.getTelContacto() + "\n" + t.getCelContacto()+ "\n" + t.getCorreoContacto());
parametros.put("nmContacto", t.getNmContacto());
parametros.put("telContacto", t.getTelContacto());
parametros.put("celContacto", t.getCelContacto());
parametros.put("correoContacto", t.getCorreoContacto());
}
}
//Se muestra el reporte
JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JREmptyDataSource());
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.exportReport();
}
catch (IOException | JRException e)
{
}
}
}
This is the Server class:
public class Server {
@JsonProperty ("contactServer")
private List contactServer = new ArrayList ();
@JsonIgnore
private Map additionalProperties = new HashMap ();
/ **
*
* @return
* The contactServer
* /
@JsonProperty ("contactServer")
public List getContactoServer () {
return contactServer;
}
/ **
*
* @param contactServer
* The contactServer
* /
@JsonProperty ("contactServer")
public void setContactoServer (List contactoServer) {
this.contactoServer = contactoServer;
}
@JsonAnyGetter
public Map getAdditionalProperties () {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty (String name, Object value) {
this.additionalProperties.put (name, value);
}
}
ContactServer Class
@JsonInclude (JsonInclude.Include.NON_NULL)
@Generated ("org.jsonschema2pojo")
@JsonPropertyOrder ({
"nmContact",
"telContact",
"celContacto",
"MailContact",
})
public class ContactServer {
@JsonProperty ("nmContact")
private String nmContact;
@JsonProperty ("telContacto")
private String telContact;
@JsonProperty ("celContacto")
private String celContacto;
@JsonProperty ("Contact mail")
private String mailContact;
@JsonIgnore
private Map additionalProperties = new HashMap ();
/ **
*
* @return
* The nmContact
* /
@JsonProperty ("nmContact")
public String getNmContact () {
return nmContact;
}
/ **
*
* @param nmContact
* The nmContact
* /
@JsonProperty ("nmContact")
public void setNmContacto (String nmContacto) {
this.nmContact = nmContact;
}
/ **
*
* @return
* The telContact
* /
@JsonProperty ("telContacto")
public String getTelContact () {
return telContact;
}
/ **
*
* @param telContact
* The telContact
* /
@JsonProperty ("telContacto")
public void setTelContacto (String telContacto) {
this.telContact = telContact;
}
/ **
*
* @return
* The celContact
* /
@JsonProperty ("celContacto")
public String getCelContact () {
return celContact;
}
/ **
*
* @param celContact
* The celContact
* /
@JsonProperty ("celContacto")
public void setCelContacto (String celContacto) {
this.celContact = celContact;
}
/ **
*
* @return
* The mailContact
* /
@JsonProperty ("Contact mail")
public String getCorreoContacto () {
return mailContact;
}
/ **
*
* @param mailContact
* The mailContact
* /
@JsonProperty ("Contact mail")
public void setCorreoContacto (String correoContacto) {
this.mailContact = mailContact;
}
@JsonAnyGetter
public Map getAdditionalProperties () {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty (String name, Object value) {
this.additionalProperties.put (name, value);
}
}