【问题标题】:Why does java springboot print an empty json in the browser?java spring boot为什么会在浏览器中打印一个空的json?
【发布时间】:2020-10-04 05:34:18
【问题描述】:

Java Rest Api 在http://localhost:8080/books 上打印

[{},{},{}]

而不是 booklist 对象。我使用主要方法 book_controller 和书籍模型。首先,我在 getbooks() 方法的列表中添加几本书,然后将它们作为列表返回。

为什么会这样?

应用程序.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;

@SpringBootApplication (exclude = SecurityAutoConfiguration.class)
public class Lab6NosApplication {

public static void main(String[] args) {
    SpringApplication.run(Lab6NosApplication.class, args);
}
}

Book_controller.java

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import ch.qos.logback.classic.Logger;
import net.minidev.json.JSONArray;

@Controller
public class Book_controller  implements ErrorController, Serializable {

@GetMapping("books")
public @ResponseBody List<book> getbooks() {
    List<book> bookList = new ArrayList<book>();
    bookList.add(new book(1,"lokesh","gupta"));
    bookList.add(new book(2,"lokesh","gupta"));
    bookList.add(new book(3,"lokesh","gupta"));

    java.lang.System.out.print(bookList);

    return bookList;
}

  @RequestMapping("/error")
  @ResponseBody
  public String handleError(HttpServletRequest request) {
      Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
      Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
      return String.format("<html><body><h2>Error Page</h2><div>Status code: <b>%s</b></div>"
                      + "<div>Exception Message: <b>%s</b></div><body></html>",
              statusCode, exception==null? "N/A": exception.getMessage());
  }

  @Override
  public String getErrorPath() {
      return "/error";
  }
}

Book.java

public class Book {

   public Book(Integer id, String title, String author) {
      super();
      this.id = id;
      this.title = title;
      this.author = author;
   }

   private Integer id;
   private String title;
   private String author;

   //getters and setters

   @Override
   public String toString() {
      return "Employee [id=" + id + ", title=" + title
            + ", author=" + author + "]";
   }
}

谢谢!

【问题讨论】:

  • 您能否尝试从控制器中删除 @JsonSerialize 并将 book 重命名为 Book 并将 book_controller 重命名为 BookController 以遵循一些基本的 Java 命名约定
  • 感谢 rieckpil 的创意!我更改了代码并更新了上面的文本,但不幸的是端点没有任何变化

标签: java json list spring-boot rest


【解决方案1】:

您看到它是空的,因为您的 Book 类没有公共 getter 或属性,因此序列化程序将无法访问它的值。

将 getter 添加到您的 Book 类:

public Integer getId(){
    return this.id;
}
public String getTitle(){
    return this.title;
}
public String getAuthor(){
    return this.author;
}

您还可以改进您的代码,使用更好的命名,例如 BookController 而不是 Book_controller。看看java命名约定。 最后,看一下@RestController。如果使用@RestController,则不需要@ResponseBody

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2017-04-04
    • 1970-01-01
    • 2017-03-23
    • 2020-01-11
    • 1970-01-01
    • 2015-05-18
    • 2022-11-06
    • 2013-02-09
    相关资源
    最近更新 更多