Spring Boot - десериализация объекта внутри списка объектов - PullRequest
0 голосов
/ 09 декабря 2018

У меня есть объект как вопрос.Внутри объекта вопроса у меня есть список тем.Когда я пытаюсь отправить запрос на этот контроллер, я получаю как список тем, как пустой.Я пытался проверить пользовательский интерфейс чванства, я не мог видеть никаких аргументов в списке.

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 Тема реализует 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;
}

}

Spring Boot Class

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

Swagger-UI запрашиваемый объект

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

Ожидаемый запрос

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

Ответы [ 2 ]

0 голосов
/ 09 декабря 2018

У вас просто нет методов получения и установки, так что если вы собираетесь возвращать данные в весенний MVC и собираетесь передавать объекты, которые вы определяете сами, то вам следует добавить методы получения и установкиСпособы впрыска в пружину 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;
    }
}

Вы понимаете?

0 голосов
/ 09 декабря 2018

В каждом поле отсутствуют методы получения / установки.

@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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...