通过Spring Boot API在REST调用中发布oneToMany

问题描述 投票:0回答:2

当我从单个REST调用POST以创建库并为库关联Books时,我遇到了问题。已创建库记录,但相关书籍不是。图书馆和书籍有一种关系。我的POST请求和响应如下 -

邮政 - http://localhost:8080/libraries/

REQUEST BODY
{
    "name":"My Library",
    "books": [
        {"title": "Effective Java", "isbn": "1234"},
        {"title": "Head First Java", "isbn": "5678"}
        ]
}
REPOSNSE 
1

POST后获得图书馆 - http://localhost:8080/libraries/

[
    {
        "id": 1,
        "name": "My Library",
        "books": [],
        "address": null
    }
]

POST to create Library and add Books GET REQUEST for Libraries

楷模

package com.publiclibrary.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.springframework.data.rest.core.annotation.RestResource;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
public class Library {

    @Id
    @GeneratedValue
    private long id;

    @Column
    private String name;

    @OneToMany(mappedBy = "library")
    private List<Book> books;

    @OneToOne
    @JoinColumn(name = "address_id")
    @RestResource(path = "libraryAddress", rel="address")
    private Address address;

    // standard constructor, getters, setters
    public Library(String name) {
        super();
        this.name = name;
    }

}
package com.publiclibrary.domain;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
//@Builder
//@AllArgsConstructor
@Entity
public class Book {

    @Id
    @GeneratedValue
    private long id;

    @NotNull
    private String title, isbn;

    @ManyToOne
    @JoinColumn(name="library_id")
    private Library library;    


    @ManyToMany(mappedBy = "books")
    private List<Author> authors;
}

REPOSITORY

package com.publiclibrary.repo;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.publiclibrary.domain.Book;

@RepositoryRestResource(path = "books", collectionResourceRel = "books")
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {

}
package com.publiclibrary.repo;

import org.springframework.data.repository.CrudRepository;

import com.publiclibrary.domain.Library;

public interface LibraryRepository extends CrudRepository<Library, Long> {
}

服务

package com.publiclibrary.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.publiclibrary.domain.Library;
import com.publiclibrary.repo.LibraryRepository;

@Service
public class LibraryService {

    @Autowired
    LibraryRepository libraryRepository;

    public List<Library> getAllLibrarys() {
        List<Library> librarys = new ArrayList<Library>();
        libraryRepository.findAll().forEach(library -> librarys.add(library));
        return librarys;
    }

    public Library getLibraryById(long id) {
        return libraryRepository.findById(id).get();
    }

    public void saveOrUpdate(Library library) {
        libraryRepository.save(library);
    }

    public void delete(long id) {
        libraryRepository.deleteById(id);
    }
}

RESTCONTROLLER

package com.publiclibrary.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.publiclibrary.domain.Library;
import com.publiclibrary.service.LibraryService;

@RestController
public class LibraryController {

    @Autowired
    LibraryService libraryService;

    @GetMapping("/libraries")
    private List<Library> getAllLibrarys() {
        return libraryService.getAllLibrarys();
    }

    @GetMapping("/libraries/{id}")
    private Library getLibrary(@PathVariable("id") int id) {
        return libraryService.getLibraryById(id);
    }

    @DeleteMapping("/libraries/{id}")
    private void deleteLibrary(@PathVariable("id") int id) {
        libraryService.delete(id);
    }

    @PostMapping("/libraries")
    private long saveLibrary(@RequestBody Library library) { 
        libraryService.saveOrUpdate(library);
        return library.getId(); 
    }

}

如何按照我的意图创建图书馆并添加图书?感谢任何帮助!

java json rest spring-boot one-to-many
2个回答
0
投票

尝试在Library类中的books集合中添加cascade persist(或者更好的只是级联all)。例如。

@OneToMany(fetch = FetchType.LAZY, mappedBy = "library", cascade = CascadeType.ALL)
private List<Book> books;

0
投票

我跟着this article解决了这个问题。我明确地处理了解析JSON并创建我的数据对象。另外,我在parent(Library)类中添加了添加和删除方法,并定义了equals和hashcode,原因在上面的链接中有解释。

我的代码更改如下 -

图书馆 -

    @OneToMany(mappedBy = "library", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnoreProperties("library")
private List<Book> books = new ArrayList<>();

public void addBook(Book book) {
    books.add(book);
    book.setLibrary(this);
}

public void removeBook(Book book) {
    books.remove(book);
    book.setLibrary(null);
}

书 -

@JsonIgnoreProperties("books")
private Library library;    

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Book )) return false;
    return id != null && id.equals(((Book) o).id);
}
@Override
public int hashCode() {
    return 31;
}

LibraryController -

    @PostMapping("/libraries")
private long saveLibrary(@RequestBody Map<String, Object> payload) {
    Library library = new Library();
    library.setName(payload.get("name").toString());

    @SuppressWarnings("unchecked")
    List<Map<String, Object>> books = (List<Map<String, Object>>) payload.get("books");
    for (Map<String, Object> bookObj : books) {
        Book book = new Book();
        book.setTitle(bookObj.get("title").toString());
        book.setIsbn(bookObj.get("isbn").toString());
        library.addBook(book);
    }

    libraryService.saveOrUpdate(library);

    return library.getId(); 
}
© www.soinside.com 2019 - 2024. All rights reserved.