Chirayu Singhvi Chirayu Singhvi - 4 months ago 15
JSON Question

How to rename json objects(variables) name in spring boot

Hello I am new to spring boot and JSON and need help in renaming the variable name coming in response.

Consider this as a input

"1": {
"id": "1",
"firstName": "Cdgdghirayu",
"lastName": "Sinfsdgdgghvi",
"age": 23
}


Now in respone I need in this format

"1": {
"Roll number": "1",
"firstName": "Cdgdghirayu",
"lastName": "Sinfsdgdgghvi",
"age": 23
}


can we map
"id"
to
"roll number"
in some way?
How to achieve this in spring-boot?

Thanks in advance

upadte-
This is my 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();
// TODO Auto-generated constructor stub
}
/**
* @param id
* @param firstName
* @param lastName
* @param age
*/
public Person(String id, String firstName, String lastName, int age) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}





/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
}



This is my service class
Here in addPerson() function I have to set id as its taking id as 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){
// person.setId("1");
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 all person details");
System.out.println("syso is 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 with 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 a partcular person details with id "+id);
return personService.updatePerson(person);
}


@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
public void removePerson(@PathVariable("id") String id){
log.info("Removing the person details with id "+id);
personService.removePerson(id);
}

@RequestMapping("/{id}")
public Person getPerson(@PathVariable("id") String id){
log.info("getting person details with id "+id);
return personService.getPerson(id);
}

public void setDthDao(PersonService dao) {//for testing purpose
// TODO Auto-generated method stub
personService=dao;
}



Now in SOAPUI testing(writing text as snapshots are not uploading)

for GET 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 for Post- : endpoint-http://localhost:8080 :Resource-/persons/all

Mediatype-


{
"firstName": "Rahul",
"lastName": "Jain",
"age": 23,
"id": "6"
}



Response what I am getting


{
"timestamp": 1469790685110,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.NullPointerException",
"message": "No message available",
"path": "/persons/all"
}



Response that I want


{
"firstName": "Rahul",
"lastName": "Jain",
"age": 23,
"Roll number": "6"
}


Answer

Assuming you've getter method for id property, if you did then try adding com.fasterxml.jackson.core dependency to your project and then annotating your method like this:

@JsonProperty("Roll number") public Long getId (){..}