【问题标题】:Name Tag missing in JSON generated by springbootspringboot生成的JSON中缺少名称标签
【发布时间】:2018-10-20 20:05:01
【问题描述】:

我遇到了以下代码的问题。以下代码生成 json 如下:

[{"id":123,"subjects":"English"},{"id":456,"subjects":"Maths"}]

我们可以看到生成的 JSON 中缺少名称标签。我不明白当我返回学生列表时,生成的 json 应该包含名称标签,因为 Student 类中有一个“名称”类型的属性。请帮忙!

PS:我已经在调试中获取了代码,我看到“students arraylist”0 索引和索引确实包含名称属性以及 id 和主题。但 JSON 不包含此属性。

休息控制器:

package com.example.demo;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


@SpringBootApplication
@RestController
public class Demo1Application {


@Autowired
Student student;
List<Student> students = new ArrayList<>();


@RequestMapping(value="/getStudents", method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public List<Student> getstudent () { 
    students = student.setStudent();
    return students;
}


public static void main(String[] args) {
    SpringApplication.run(Demo1Application.class, args);
    System.out.println("Hello");
}
}

学生豆类:

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Student {
    int id;
    String Subject;

    @Autowired
    @Qualifier("name")
    Name name;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getSubjects() {
        return Subject;
    }
    public void setSubject(String subject) {
        Subject = subject;
    }

    public void setName(String Fname, String Mname, String Lname) {
        Name nm = new Name();  
        name = nm.setfullname(Fname,Mname,Lname) ;
    }

    public Student(int id, String Subject) {
        this.id = id;
        this.Subject = Subject;
    }

    public Student() {
        System.out.println("Student Object created");
    }

    public List<Student> setStudent() {
        List<Student> Students = new ArrayList<>(); 
        Student s = new Student ();
        Student s1 = new Student ();


        s.setId(123);
        s.setSubject("English");
        s.setName("Hemant", "123", "Mamod");

        s1.setId(456);
        s1.setSubject("Maths");
        s1.setName("Akshay", "123", "pandey");

        Students.add(s);
        Students.add(s1);
        return Students;
      }
    }

命名 Bean 类:

package com.example.demo;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
@Qualifier("name")
public class Name {
    String Firstname;
    String Middlename;
    String Lastname;

    public Name() {
        System.out.println("Name Object created");
    }

    public String getFirstname() {
        return Firstname;
    }
    public void setFirstname(String firstname) {
        Firstname = firstname;
    }
    public String getMiddlename() {
        return Middlename;
    }
    public void setMiddlename(String middlename) {
        Middlename = middlename;
    }
    public String getLastname() {
        return Lastname;
    }
    public void setLastname(String lastname) {
        Lastname = lastname;
    }

    public Name setfullname(String Fname, String Mname, String Lname) {
        Firstname = Fname;
        Middlename = Mname;
        Lastname = Lname;
        return this;
    }

}

【问题讨论】:

  • 为什么要在Student.setName() 方法中创建一个新的Name 实例?由于 Name 是一个 Spring bean,它会在 Student 类中自动装配,它将在 Spring 上下文中自动创建
  • @YuriyTsarkov 是的。但是通过将 Name.setfullname() 设为 void 并调用 setfullname 作为 name.setfullname(),我得到了 Null 指针异常。所以我绕道而行。

标签: java spring spring-boot annotations spring-restcontroller


【解决方案1】:

问题是您在Student 类中没有Name 的getter。 请按如下方式添加getter:

@Component
public class Student {
  int id;
  String Subject;

  Name name;

  /******/

  public Name getName() {
    return name;
  }
}

谈到带有name 属性的NullPinterException - 这意味着由于某些原因它没有被自动装配,所以在您的情况下,您可以删除@Autowired@Qualifier 注释或找出问题所在自动装配

【讨论】:

  • 哇!!它就像一个魅力!但是发生了什么变化?我不明白名称的获取者如何使这件事起作用!请解释
  • 问题是JsonMapper 需要public 属性或方法来确定哪些属性必须被序列化。所以在我们的例子中,我们只是添加了一个公共方法,JsonMapper 可以将它包含到必须序列化的属性列表中。从理论上讲,您可以将public 分类器添加到name 属性并获得相同的结果。以下是有关主题Jackson – Decide What Fields Get Serialized/Deserialized 的一些信息
  • 我在反序列化方面也遇到了一些问题。请你看看这个:stackoverflow.com/questions/53571435/…
  • 请查看以上链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 2018-03-19
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多