我正在嘗試使用 GSON 獲得清晰的 JSON 物件(書籍)陣列。但是每當我運行以下代碼時:
public class Book {
private int id;
private String name;
private String type;
private boolean available;
public Book(int id, String name, String type, boolean available) {
super();
this.id = id;
this.name = name;
this.type = type;
this.available = available;
}
@Override
public String toString() {
return "{id=" id ", name=" name ", type=" type ", available=" available "},";
}
}
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
@Component
public class BookService {
private int size = 0;
public int len = 6;
public Book Allbooks[] = new Book[len];
public Book AddBook(int id, String name, String type, boolean available) {
if(id == len)
return null;
this.size ;
return Allbooks[this.size - 1] = createBook(id, name, type, available);
}
private Book createBook(int id, String name, String type, boolean available) {
return new Book(id, name, type, available);
}
public Book[] bookRepo() {
Allbooks[0] = AddBook(1, "One", "fiction", false);
Allbooks[1] = AddBook(2, "Two", "non-fiction", true);
Allbooks[2] = AddBook(3, "Three", "fiction", true);
Allbooks[3] = AddBook(4, "Four", "fiction", false);
Allbooks[4] = AddBook(5, "Five", "non-fiction", true);
Allbooks[5] = AddBook(6, "Six", "non-fiction", true);
return Allbooks;
}
public String[] showAll() {
String[] arr = new String[Allbooks.length];
for (int i = 0; i < arr.length; i ) {
arr[i] = new Gson().toJson(Allbooks[i], Book.class);
}return arr;
}
}
@RestController
class ControllerGET {
@Autowired Status status;
@Autowired BookService bookRepository;
@GetMapping(path = {"/", ""})
public String mainPage() {
return "Welcome to Mile's simple book-API";
}
@GetMapping(path = "/status")
public Status getStatus() {
status.value = "OK";
return status;
}
@GetMapping(path = "/books")
public String[] getAllBooks() {
bookRepository.bookRepo();
return bookRepository.showAll();
}
//@GetMapping(path = "/books/:bookid")
}
我在本地主機(郵遞員)上得到以下輸出:
[
"{\"id\":1,\"name\":\"One\",\"type\":\"fiction\",\"available\":false}",
"{\"id\":2,\"name\":\"Two\",\"type\":\"non-fiction\",\"available\":true}",
"{\"id\":3,\"name\":\"Three\",\"type\":\"fiction\",\"available\":true}",
"{\"id\":4,\"name\":\"Four\",\"type\":\"fiction\",\"available\":false}",
"{\"id\":5,\"name\":\"Five\",\"type\":\"non-fiction\",\"available\":true}",
"null"
]
我的目標是讓它看起來像這樣:
{"id":1,
"name":"One",
"type":"fiction",
"available":false}",
}
其他書也一樣。如果您知道方法,請幫助我。我將不勝感激!……………………………………………………………………………………………………………………………… ……………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………… ………………
uj5u.com熱心網友回復:
在回傳 springboot 之前,您不需要決議為 json。Spring boot 會為你做這件事(默認使用 jackson lib)
嘗試這個:
@GetMapping(path = "/books")
public Book[] getAllBooks() { // return Book[] instead of String []
bookRepository.bookRepo();
return bookRepository.bookRepo(); // return all list of books
}
uj5u.com熱心網友回復:
洗掉@Override toString()函式。直接回傳gson().toJson()字串。
然而這并不好。您可以定義ArrayList而不是原始陣列。所以,
ArrayList<Book> AllBooks;
AllBooks.add(new Book(// add book 1));
AllBooks.add(new Book(// add book 2));
AllBooks.add(new Book(// add book 3));
然后在 repo 上你可以回傳
return new Gson.toJson(AllBooks);
然而
這不是最佳做法,因為您的書籍存盤在服務器的RAM 上。所以,重新考慮建立一個資料庫來輔助資料關系。事實上,spring 提供了 jpa@Entity來幫助你處理這種事情。當您將圖書宣告為物體時,例如
@Entity
public class Book {
private int id;
private String name;
private String type;
private boolean available;
}
您可以創建一個JpaRepository來檢索所有資料,例如
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
}
然后只需呼叫控制器上的存盤庫
@GetMapping(path = "/books")
public List<book> getAllBooks() {
BookRepository bookRepo = new BookRepository();
List<book> books = bookRepo.findAll();
return books;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/321389.html
上一篇:包火花不存在
下一篇:將slf4j-api-2.0.0-alpha1.jar添加到專案結構并將slf4j依賴項添加到pom.xml后,“未找到SLF4J提供程式”
