2016-07-28 49 views
2

你好我是新的彈簧啓動和JSON,需要幫助重新命名變量名稱作爲響應。如何在彈簧啓動時重命名json對象(變量)的名稱

輸入反應中,我需要這種格式考慮以此爲輸入

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

現在

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

我們可以以某種方式映射到"id""roll number"? 如何在spring-boot中實現這個功能?

在此先感謝

upadte- 這是我的模型類

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; 
} 
} 

這是我的服務類 在這裏,在addPerson的()函數,我必須設置id作爲其採取ID爲空。 。

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); 
    } 

我的控制器類

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 
    personService=dao; 
    } 
在SOAPUI測試

現在的GET請求(寫入文本快照不會上傳)

:endpoint- http://localhost:8080:資源 - /人/所有

應答爲

{ 
    "2": { 
     "firstName": "Himanshu", 
     "lastName": "Singhvi", 
     "age": 20, 
     "Roll Number": "2" 
    }, 
    "1": { 
     "firstName": "Chirayu", 
     "lastName": "Singhvi", 
     "age": 23, 
     "Roll Number": "1" 
    } 
} 

現在對於Post-:endpoint- http://localhost:8080:Resource-/persons/all

Mediatype-

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

迴應什麼,我得到

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

迴應,我想

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

回答

5

假設你的getter方法id屬性,如果你沒有那麼嘗試加入com.fasterxml.jackson.core依賴於您的項目,然後註釋你的方法是這樣的:

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

+0

它的工作謝謝... –

+0

的一個問題,現在發生,因爲我使用@jsonProperty,當我在做POST請求爲{ 「名字」: 「Chfirayu」, 「姓氏」: 「Singghhvi」 「年齡」:23, 「id」:「6」 }它提供空ponter例外,有沒有辦法以原始格式寫後或我必須使用卷號而不是id? –

+0

我不知道你的實現的完整上下文,所以我不知道你的處理髮布請求的確切程度。你可以在問題中發佈你的實現代碼片段嗎? –

0

終於正確 只是在我的模型類的變化。

@JsonProperty("Roll Number") 
private String id; 

@JsonProperty("Roll Number") 
public String getId() { 
    return id; 
} 

@JsonProperty("id") 
public void setId(String id) { 
    this.id = id; 
}