java - 发布 spring boot 的项目列表

标签 java spring spring-boot

@RestController
public class TopicController {

    @Autowired
    private TopicService topicService;

    @RequestMapping(value="/topics", method= RequestMethod.GET)
    public List<Topic> getAllTopics(){
        return topicService.getAllTopics();
    }

    @RequestMapping(value="/topics/{id}", method= RequestMethod.GET)
    public Topic getTopic(@PathVariable String id){
        return topicService.getTopic(id);
    }

    @RequestMapping(value="/topics", method= RequestMethod.POST)
    public void addTopic(@RequestBody Topic topic){
        topicService.addTopic(topic);
    }

    @RequestMapping(value="/topics/{id}", method= RequestMethod.PUT)
    public void updateTopic(@RequestBody Topic topic, @PathVariable String id){
        topicService.updateTopic(id, topic);
    }

    @RequestMapping(value="/topics/{id}", method= RequestMethod.DELETE)
    public void deleteTopic(@PathVariable String id){
        topicService.deleteTopic(id);
    }
}

Controller 类

@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepo;

    public List<Topic> getAllTopics(){
        return (List<Topic>)topicRepo.findAll();

    }

    public Topic getTopic(String id){
        return topicRepo.findOne(id);
    }

    public void addTopic(Topic topic){
        //topics.add(topic);

        topicRepo.save(topic);
    }

    public void updateTopic(String id, Topic topic) {
        topicRepo.save(topic);
    }

    public void deleteTopic(String id) {
        //topics.removeIf(t -> t.getId().equals(id));

        //topics.removeIf((Topic t) -> t.getId().equals(id));

        topicRepo.delete(id);

    }

}

服务等级

@Repository
public interface TopicRepository extends CrudRepository<Topic, String>{

    //List<Course> findByTopic_Id(String topicid);

}

存储库类

@Entity
public class Topic {

    @Id
    @Column(name="TOPIC_ID")
    private String id;
    @Column(name="NAME")
    private String name;
    @Column(name="DESCRIPTION")
    private String description;

    @OneToMany(mappedBy="topic", fetch = FetchType.EAGER)
    @JsonManagedReference
    private List<Course> course = new ArrayList<Course>();

    //no - argument constructor. Needed for hibernate
    public Topic(){};

    public Topic(String id, String name, String description, List<Course> course){
        super();
        this.id = id;
        this.name = name;
        this.description = description;
        this.course = course;
    }

    public Topic(String id, String name, String description){
        super();
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<Course> getCourse() {
        return course;
    }

    public void setCourse(List<Course> course) {
        this.course = course;
    }
}

主题类

@Entity
public class Course{

    @Id
    @Column(name="COURSE_ID")
    private String id;
    private String name;
    private String description;

    //There could be many courses related to 1 topic
    @ManyToOne
    @JoinColumn(name = "TOPIC_ID")
    @JsonBackReference
    private Topic topic;

    public Course(){};


    public Course(String id, String name, String description){

        super();
        this.id = id;
        this.name = name;
        this.description = description;

    }

    public Topic getTopic() {
        return topic;
    }

    public void setTopic(Topic topic) {
        this.topic = topic;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

类(class)类别

我正在尝试使用 Postman 将包含许多类(class)的主题类发布到我的 SQL 数据库中。

在 Postman 中,我像这样使用 JSON 进行 POST

{
    "id": "700",
    "name": "How to countt",
    "description": "Counting numbersssss",
    "course": [
      {
        "id": "1",
        "name": "php",
        "description": "gooddddyyyy stuff"
      },
      {
        "id": "2",
        "name": "phpp",
        "description": "gooddddyyyy stuffp"
      }
    ]
}

但是,当我执行相应的获取所有主题时,我的回应是

{
    "id": "700",
    "name": "How to countt",
    "description": "Counting numbersssss",
    "course": []
}

它没有获取我发布的类(class)。一个主题可以有多个类(class)。我该如何解决?谢谢

最佳答案

您从未设置双向关联的拥有方:Course.topic

并且Topic.courses上没有设置级联。

因此,保存主题不仅不会保存其类(class),而且即使保存了,类(class)也不会属于其主题。

关于java - 发布 spring boot 的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43830080/

相关文章:

java - CompositeDisposable 导致内存泄漏,直到清除或处置调用?

Java 和 Oracle - 将 JAR 加载到 Oracle 还是单独执行?

java - MIDlet-1。 NetBeans 中的错误

java - org.springframework.beans.factory.BeanCreationException : Error creating bean with name - Spring security

spring - 基本身份验证不适用于多个配置

java - 从其他类获取 spinner 值并将其分配给 editText

java - springrabbitmq 获取对扇出消息的所有回复

java - Java 文件中的 Spring 国际化错误

java - 无法模拟服务以使其抛出异常

spring - 是否有必要使用 spring initializr 来创建 spring 应用程序?