【问题标题】:How to rename json objects(variables) name in spring boot如何在spring boot中重命名json对象(变量)名称
【发布时间】:2016-12-02 19:01:55
【问题描述】:

您好,我是 Spring Boot 和 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 中实现这一点?

提前致谢

更新- 这是我的模型课

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

我的控制器类

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:Resource-/persons/all

响应-

{
   "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

媒体类型-

{
  "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"
}

【问题讨论】:

    标签: json rest spring-boot jackson jax-rs


    【解决方案1】:

    假设您有一个用于 id 属性的 getter 方法,您可以尝试将 com.fasterxml.jackson.core 依赖项添加到您的项目并像这样注释您的方法:

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

    【讨论】:

    • 现在出现了一个问题,因为我正在使用 @jsonProperty ,当我以 { "firstName": "Chfirayu", "lastName": "Singghhvi", "age": 23, " 进行发布请求时id": "6" } 它给出了 null ponter 异常,有没有办法以原始格式写帖子,或者我必须使用卷号而不是 id?
    • 我不知道您实现的完整上下文,所以我不知道您如何处理发布请求。您能否在问题中发布您的实现 sn-p。
    • 对,您可以尝试将 @JsonProperty 放在 getter 上,而不是在您声明 id 时再试一次。
    • 试过..不工作..如果我们在正文中发送“卷号”而不是“id”,它将起作用......
    • 问题已解决...这是@JsonProperty("Roll Number") private String id; @JsonProperty("Roll Number") public String getId() { return id; } @JsonProperty("id") public void setId(String id) { this.id = id; }
    【解决方案2】:

    终于搞定了 刚刚在我的模型类中进行了以下更改。

    @JsonProperty("Roll Number")
    private String id;
    
    @JsonProperty("Roll Number")
    public String getId() {
        return id;
    }
    
    @JsonProperty("id")
    public void setId(String id) {
        this.id = id;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 1970-01-01
      • 2022-01-20
      • 2013-06-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      相关资源
      最近更新 更多