繁体   English   中英

Spring Boot-在对象列表中反序列化对象

[英]Spring Boot - deserializing Object inside list of objects

我有一个问题。 在问题对象内部,我有主题列表。 当我尝试发布对此控制器的请求时,我的主题列表为空。 我试图验证摇摇欲坠的用户界面,我看不到列表中的任何参数。

Question.java

import java.io.Serializable;
import java.util.Date;
import java.util.List; 
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull; 
import org.hibernate.annotations.CreationTimestamp; 
@Entity
public class Question implements Serializable { 
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name="questionId")
private Integer questionId;

@NotNull
@Column(name="title")
private String title;

@NotNull
@Column(name="body")
private String body;

@CreationTimestamp
private Date createdAt;

@CreationTimestamp
private Date modifiedAt;

@OneToOne
@JoinColumn(name="userId")
private User userId;

@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="question_id")
private List<Comment> commentList;

@ManyToMany(cascade=CascadeType.ALL)  
@JoinTable(name="Question_topic", 
joinColumns=@JoinColumn(name="question_id"), 
inverseJoinColumns=@JoinColumn(name="topic_id"))
private List<Topic> topicList;


/**
 * @return the questionId
 */
public Integer getQuestionId() {
    return questionId;
}

/**
 * @param questionId the questionId to set
 */
public void setQuestionId(Integer questionId) {
    this.questionId = questionId;
}

/**
 * @return the title
 */
public String getTitle() {
    return title;
}

/**
 * @param title the title to set
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 * @return the body
 */
public String getBody() {
    return body;
}

/**
 * @param body the body to set
 */
public void setBody(String body) {
    this.body = body;
}

/**
 * @return the createdAt
 */
public Date getCreatedAt() {
    return createdAt;
}

/**
 * @param createdAt the createdAt to set
 */
public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
}

/**
 * @return the modifiedAt
 */
public Date getModifiedAt() {
    return modifiedAt;
}

/**
 * @param modifiedAt the modifiedAt to set
 */
public void setModifiedAt(Date modifiedAt) {
    this.modifiedAt = modifiedAt;
}

/**
 * @return the userId
 */
public User getUserId() {
    return userId;
}

/**
 * @param userId the userId to set
 */
public void setUserId(User userId) {
    this.userId = userId;
}

/**
 * @return the commentList
 */
public List<Comment> getCommentList() {
    return commentList;
}

/**
 * @param commentList the commentList to set
 */
public void setCommentList(List<Comment> commentList) {
    this.commentList = commentList;
}

public List<Topic> getTopicList() {
    return topicList;
}

public void setTopicList(List<Topic> topicList) {
    this.topicList = topicList;
}

}

Topic.java

 import java.io.Serializable;
 import java.util.Date; 
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.OneToOne;

import org.hibernate.annotations.CreationTimestamp;

@Entity公共类Topic实现了Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column
private Integer topicId;

@Column
private String topicName;

@Column
private String topicDesc;

@CreationTimestamp
private Date createdAt;

@OneToOne
@JoinColumn(name="userId",insertable=false,updatable=false)
private User userId;



/**
 * @return the topicId
 */
public Integer getTopicId() {
    return topicId;
}

/**
 * @param topicId the topicId to set
 */
public void setTopicId(Integer topicId) {
    this.topicId = topicId;
}

/**
 * @return the topicName
 */
public String getTopicName() {
    return topicName;
}

/**
 * @param topicName the topicName to set
 */
public void setTopicName(String topicName) {
    this.topicName = topicName;
}

/**
 * @return the topicDesc
 */
public String getTopicDesc() {
    return topicDesc;
}

/**
 * @param topicDesc the topicDesc to set
 */
public void setTopicDesc(String topicDesc) {
    this.topicDesc = topicDesc;
}

/**
 * @return the createdAt
 */
public Date getCreatedAt() {
    return createdAt;
}

/**
 * @param createdAt the createdAt to set
 */
public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;
}

/**
 * @return the userId
 */
public User getUserId() {
    return userId;
}

/**
 * @param userId the userId to set
 */
public void setUserId(User userId) {
    this.userId = userId;
}

}

春季靴类

@RestController
@RequestMapping("/qna/")
public class controller{
   public ResponseEntity<?> postQuestion(@RequestBody Question){
  }
 }

Swagger-UI请求的对象

{
   "title": "abc",
   "topicList": []
}

预期要求

{
   "title": "abc",
   "topicList": [
         {
            "topicName": "topic1",
            "createdAt": ""
         }
      ]
}

在每个字段上都缺少getter / setter方法。

@Entity
public class Question implements Serializable { 

   private String title;
   private List<Topic> topicList;

   public String getTitle() {
       return title;
   }

   public void setTitle(String title) {
       this.title = title;
   }

   public List<Topic> getTopicList() {
       return topicList;
   }

   public void setTopicList(List<Topic> topicList) {
       this.topicList = topicList;
   }
}


public class Topic implements Serializable {

    private String topicName;
    private Date createdAt;

    public String getTopicName() {
        return topicName;
    }

    public void setTopicName(String topicName) {
        this.topicName = topicName;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }
}

您只是没有getter和setter方法,因此,如果要在Spring MVC中返回数据,并且要传输自己定义的对象,则应该添加getter和setter方法进行注入进入春季MVC,就像那样

@Entity
public class Question implements Serializable { 

    private String title;
    private List<Topic> topicList;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List<Topic> getTopicList() {
        return topicList;
    }

    public void setTopicList(List<Topic> topicList) {
        this.topicList = topicList;
    }
}

你理解吗?

列表<object> null 在 Spring 启动 Z594C103F2C6E04C3D8AB059F031E0C1 中反序列化 json 时<div id="text_translate"><p>我在 Spring 引导应用程序中遇到了 controller 问题。 当我在 controller(URI/调用)上进行调用时,object 列表始终是 null。</p><pre> { "code": "TEST", "name": "TEST NAME", "groupe": "A1", "list": [ {"type":"web", "link":"https://google.com/"}, {"type":"web", "link":"https://google2.com/"} ] }</pre><pre class="lang-java prettyprint-override"> @PostMapping(value="/call") public ResponseEntity&lt;Void&gt; ajouterEnvironnement(@RequestBody First first) { first.getCode() // value: "TEST" first.getList() // value: null }</pre><pre class="lang-java prettyprint-override"> public class First { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String code; private String name; private String groupe; @OneToMany(mappedBy = "first", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List&lt;Second&gt; list; }</pre><pre class="lang-java prettyprint-override"> public class Second { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String type; private String link; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "first_id", nullable = false) private First first; }</pre></div></object>

[英]List<object> null when deserializing json in Spring Boot controller

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Spring Boot到具有通用对象列表的POJO中反序列化JSON 列表<object> null 在 Spring 启动 Z594C103F2C6E04C3D8AB059F031E0C1 中反序列化 json 时<div id="text_translate"><p>我在 Spring 引导应用程序中遇到了 controller 问题。 当我在 controller(URI/调用)上进行调用时,object 列表始终是 null。</p><pre> { "code": "TEST", "name": "TEST NAME", "groupe": "A1", "list": [ {"type":"web", "link":"https://google.com/"}, {"type":"web", "link":"https://google2.com/"} ] }</pre><pre class="lang-java prettyprint-override"> @PostMapping(value="/call") public ResponseEntity&lt;Void&gt; ajouterEnvironnement(@RequestBody First first) { first.getCode() // value: "TEST" first.getList() // value: null }</pre><pre class="lang-java prettyprint-override"> public class First { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String code; private String name; private String groupe; @OneToMany(mappedBy = "first", cascade = CascadeType.ALL, fetch = FetchType.EAGER) private List&lt;Second&gt; list; }</pre><pre class="lang-java prettyprint-override"> public class Second { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String type; private String link; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "first_id", nullable = false) private First first; }</pre></div></object> Spring + Jackson +反序列化通用对象列表 Spring Boot无法返回对象的JSON,但不返回对象列表 反序列化以对象ID作为对象名称的JSON对象列表 使用 Jackson 反序列化包装在未命名根 object 内的 JSON 对象 反序列化对象列表 将 Yaml 中的列表映射到 Spring Boot 中的对象列表 将YAML列表映射到Spring Boot中的对象列表 Spring 启动,无法获取此 object 与另一个 object (@OneToOne) 相关的对象列表
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM