Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java springboot生成的JSON中缺少名称标记_Java_Spring_Spring Boot_Annotations_Spring Restcontroller - Fatal编程技术网

Java springboot生成的JSON中缺少名称标记

Java springboot生成的JSON中缺少名称标记,java,spring,spring-boot,annotations,spring-restcontroller,Java,Spring,Spring Boot,Annotations,Spring Restcontroller,我面临如下代码的问题。下面的代码生成json,如下所示: [{“id”:123,“学科”:“英语”},{“id”:456,“学科”:“数学”}] 我们可以看到生成的JSON中缺少name标记。我不明白当我返回学生列表时,结果json应该包含name标记,因为学生类中有一个类型为“name”的属性。请帮忙 PS:我在debug中获取了代码&我看到“students arraylist”0索引和索引包含名称prpoperty以及id和subject。但是JSON不包含这个属性 Restcontrol

我面临如下代码的问题。下面的代码生成json,如下所示:

[{“id”:123,“学科”:“英语”},{“id”:456,“学科”:“数学”}]

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

PS:我在debug中获取了代码&我看到“students arraylist”0索引和索引包含名称prpoperty以及id和subject。但是JSON不包含这个属性

Restcontroller:

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

问题是您在
学生
课程中没有获得
姓名
。 请按如下方式添加getter:

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

  Name name;

  /******/

  public Name getName() {
    return name;
  }
}

说到带有
名称
属性的
NullPinterException
,这意味着由于某些原因它没有自动连接,因此,在您的情况下,您可以删除一个
@Autowired
@Qualifier
注释,或者找出Autowired的问题所在

为什么要在
Student.setName()方法中创建一个新的
名称
实例?由于
名称
是Spring bean,它在
学生
类中自动连接,因此它将在Spring上下文中创建automatically@YuriyTsarkov是的,没错。但通过将Name.setfullname()设置为void&调用setfullname作为Name.setfullname(),我得到了空指针异常。所以我就这么做了。哇!!它就像一个符咒!但是什么改变了呢?我不明白一个有名气的人是怎么做到的!!请解释一下,
JsonMapper
需要
public
属性或方法来确定哪些属性必须序列化。因此,在我们的例子中,我们刚刚添加了一个公共方法,并且
JsonMapper
能够将其包含到必须序列化的属性列表中。理论上,您可以将
公共
分类器添加到
名称
属性中,并获得相同的结果。下面是关于这个主题的一些信息,我在反序列化时也面临一些问题。请你看看这个:请检查上面的链接
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;
    }

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

  Name name;

  /******/

  public Name getName() {
    return name;
  }
}