rest - How to rename json objects(variables) name in spring boot -
hello new spring boot , json , need in renaming variable name coming in response.
consider input
"1": { "id": "1", "firstname": "cdgdghirayu", "lastname": "sinfsdgdgghvi", "age": 23 }
now in respone need in format
"1": { "roll number": "1", "firstname": "cdgdghirayu", "lastname": "sinfsdgdgghvi", "age": 23 }
can map "id"
"roll number"
in way? how achieve in spring-boot?
thanks in advance
upadte- model class
package model; import javax.xml.bind.annotation.xmlrootelement; import com.fasterxml.jackson.annotation.jsonproperty; @xmlrootelement public class person { @jsonproperty("roll number") private string id; private string firstname; private string lastname; private int age; public person() { super(); } public person(string id, string firstname, string lastname, int age) { super(); this.id = id; this.firstname = firstname; this.lastname = lastname; this.age = age; } public string getid() { return id; } public void setid(string id) { this.id = id; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public int getage() { return age; } public void setage(int age) { this.age = age; } }
this service class here in addperson() function have set id taking id null..
package service; import java.util.hashtable; import org.springframework.stereotype.service; import model.person; @service public class personservice { hashtable<string,person> persons=new hashtable<string,person>(); public personservice(){ person p=new person(); p.setid("1"); p.setfirstname("chirayu"); p.setlastname("singhvi"); p.setage(23); persons.put("1",p); p=new person(); p.setid("2"); p.setfirstname("himanshu"); p.setlastname("singhvi"); p.setage(20); persons.put("2",p); } public person getperson(string id){ if(persons.containskey(id)) return persons.get(id); else return null; } public hashtable<string,person> getall(){ return persons; } public person addperson(person person){ persons.put(person.getid(),person); return person; } public person updateperson(person person){ if(person.getid().equals("0")){ return null; } persons.put(person.getid(),person); return person; } public person removeperson(string id) { return persons.remove(id); }
my controller class
package controller; import java.util.hashtable; import org.apache.log4j.logger; import org.apache.log4j.propertyconfigurator; import org.springframework.beans.factory.annotation.autowired; import org.springframework.http.mediatype; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; import org.springframework.web.bind.annotation.restcontroller; import com.fasterxml.jackson.databind.annotation.jsonnaming; import model.person; import service.personservice; @restcontroller @requestmapping("/persons") public class personcontroller { static final logger log = logger.getlogger(personcontroller.class.getname()); personcontroller(){ log.info("entering controller class"); } @autowired personservice personservice; @requestmapping("/all") public hashtable<string,person> getall(){ log.info("getting person details"); system.out.println("syso working"); return personservice.getall(); } @requestmapping(value="/all",method=requestmethod.post, produces=mediatype.application_json_value, consumes=mediatype.application_json_value ) @responsebody public person addperson(@requestbody person person){ log.info("adding person id "+person.getid()); person.getid(); return personservice.addperson(person); } @requestmapping(value="/{id}",method=requestmethod.put, produces=mediatype.application_json_value, consumes=mediatype.application_json_value ) @responsebody public person updateperson(@pathvariable("id") string id, @requestbody person person){ person.setid(id); log.info("updating partcular person details id "+id); return personservice.updateperson(person); } @requestmapping(value="/{id}",method=requestmethod.delete) public void removeperson(@pathvariable("id") string id){ log.info("removing person details id "+id); personservice.removeperson(id); } @requestmapping("/{id}") public person getperson(@pathvariable("id") string id){ log.info("getting person details id "+id); return personservice.getperson(id); } public void setdthdao(personservice dao) {//for testing purpose personservice=dao; }
now in soapui testing(writing text snapshots not uploading)
for request : endpoint-http://localhost:8080 :resource-/persons/all
response-
{ "2": { "firstname": "himanshu", "lastname": "singhvi", "age": 20, "roll number": "2" }, "1": { "firstname": "chirayu", "lastname": "singhvi", "age": 23, "roll number": "1" } }
now post- : endpoint-http://localhost:8080 :resource-/persons/all
mediatype-
{ "firstname": "rahul", "lastname": "jain", "age": 23, "id": "6" }
response getting
{ "timestamp": 1469790685110, "status": 500, "error": "internal server error", "exception": "java.lang.nullpointerexception", "message": "no message available", "path": "/persons/all" }
response want
{ "firstname": "rahul", "lastname": "jain", "age": 23, "roll number": "6" }
assuming you've getter method id property, if did try adding com.fasterxml.jackson.core dependency project , annotating method this:
@jsonproperty("roll number") public long getid (){..}
Comments
Post a Comment