Reputation: 23
I am working on Rest web services and producing JSON data for mobile app.I am using Jersey 1.4, web server as tomcat 7, Hibernate 3.3,MySQL5. I am new to REST world. I have an endpoint which simply returns a list of year based on my query.
Output:
{"success":true,"count":2,"message":null,"data":[{"year":2012},{"year":2013}]}
which is fine what I expected.
But I have been asked to include a description value of type string with this return json data set. Based on value of year.
If year=2012(current year) description should be "this year", for year=2013 description ="next year", for year= 2011 description="previous year" ,for year=2010 description="2010" and so on.
What I have to return is something like: {"success":true,"count":2,"message":null,"data":[{"year":2012,"description":"this year"},{"year":2013,"description":"next year"}]
My resource class
@GET
@Path("/{countryCd}/upcoming")
@Produces("application/json")
public JResponse<JsonResponse> getYearsForUpcoming(@PathParam("countryCd")
String countryCode) {
JobDao jobDao =new JobDao();
JsonResponse res= new JsonResponse();
@SuppressWarnings({ })
List<Jobs> jobList= jobDao.getYearsForUpcoming(countryCode);
if(jobList==null){
throw new NotFoundException("Error occured! NO data found");
}
int count = jobList.size();
res.setCount(count);
res.setData(jobList);
return JResponse.ok(res).build();
}
my query
String sql="SELECT distinct year(s_date) as year from jobs where country=:countryCode and year(s_date)>=year(curdate()) order by year(s_date) asc";
Query query=session.createSQLQuery(sql).addScalar("year").setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
query.setParameter("countryCode", countryCode);
upcomingRegattaList= query.list();
Jobs.java ( myeclipse generated entity for table jobs )
public class Jobs implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Integer jobId;
private String status;
private Date SDate;
//some more
// getter and setters
}
I am clueless how to add a new attribute to my JSON data which is not in my entity. Any idea greatly appreciated.
Thanks in advance
Upvotes: 0
Views: 336
Reputation: 11337
You have probably to create one more entity to map your new object that will be something like
public class Data {
private String year;
private String description;
}
then load a list of this entities and add them to the response.
Upvotes: 0