0

I am retrieving data from multiple tables/POJO's.I want the data in JSON format.In Pojo classes I am using @JsonProperty.Still I am not getting result Json in desired format. My result:

[["2017 Sprint 1","Android development",23/12/2016,16/01/2017]]

I want result in format { "iteration": "2017 Sprint 1", "project": "MDM - Core & Integration", "isd": "23/12/2016", "ied": "16/01/2017",

My main controller method:

@Controller
@RequestMapping("/json/retrospective")
public class MainControllerClass 
{
    @RequestMapping(value="{userid}", method = RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<Details> getInfoInJSON(@PathVariable int userid)
    {
        Configuration con = new Configuration();
        con.configure("hibernate.cfg.xml");
        SessionFactory SF = con.buildSessionFactory();
        Session session= SF.openSession();
        Query test=session.createQuery("select itr.iteration_name,prj.project_name,itr.isd,itr.ied from RetrospectiveInfo retro,IterationInfo itr,ProjectInfo prj where retro.retrospective_id ="+userid+" and retro.project_id = prj.project_id and retro.iteration_id = itr.iteration_id");
        List<Details> details= test.list();
        session.close();
        SF.close();
        return details;
    }
}

Class details:

public class Details 
{
    @JsonProperty("iteration")
    private String iteration;
    @JsonProperty("project")
    private String project;
    @JsonProperty("isd")
    private Date isd;
    @JsonProperty("ied")
    private Date ied;


getter/setters

I have got 3 Jackson jars annotation,databind and core of latest version 2.8 in buildpath.Why I am I getting such a result??What changes do I need to make in my code??Are any jars to be added??kindly help

Hitham S. AlQadheeb
  • 13,613
  • 3
  • 47
  • 74
Akshay
  • 1,127
  • 1
  • 11
  • 29

1 Answers1

0

The main issue is that your are constructing a Details class that is formed from a JPA query using different types check (Error: Cannot create TypedQuery for query with more than one return)

To resolve the issue, create a constructor for the required JSON attributes

package com.sof.type;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({ "iteration", "project", "isd", "ied"})
public class Details {

    @JsonProperty
    private String iteration;

    @JsonProperty
    private String project;

    @JsonProperty
    private String isd;

    @JsonProperty
    private String ied;

    public Details(String iteration, String project, String isd, String ied) {
        this.iteration = iteration;
        this.project = project;
        this.isd = isd;
        this.ied = ied;
    }

}

Then use it this way

@PersistenceContext
private EntityManager em;

or

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.sof");
EntityManager em = entityManagerFactory.createEntityManager();

with

List<Details> details = em.createQuery(
        "SELECT NEW  com.sof.type.Details(itr.iteration_name, prj.project_name, itr.isd, itr.ied) " +
        "FROM RetrospectiveInfo retro, " +
        "     IterationInfo itr, " + 
        "     ProjectInfo prj " +
        "WHERE retro.retrospective_id = :userId " +
        "AND retro.project_id = prj.project_id " +
        "AND retro.iteration_id = itr.iteration_id", Details.class)
    .setParameter("userId", userid)
    .getResultList();
Community
  • 1
  • 1
Hitham S. AlQadheeb
  • 13,613
  • 3
  • 47
  • 74