Servelt中 实体类是时间格式 变成JSON后 传给前台变成毫秒数 解决方法

  private int id;
    private String title;
    private String author;
    @JSONField(format="yyyy-MM-dd HH:mm:ss")
    private Date publicDate;
    private String publisher;
    private String isbn;
    private BigDecimal price;
    private String picture;
    private int cid;

直接加上 注解 什么都不动

   @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        BookServiceImpl book = new BookServiceImpl();  //书籍
         List<Book> books = book.queryAll();

        //把书籍的信息转成JSON 格式   首先创建JSONObject  然后转成String
        JSONObject jsonObject = new JSONObject();
        String bookList = jsonObject.toJSONString(books);
        // 然后把字符串传到对象中
        System.out.println(bookList);
        jsonObject.put("bookList",bookList);
        //然后通过respone 返回给页面
        PrintWriter out = resp.getWriter();
        out.write(bookList);

    }

直接返给前台就ok 就是正常时间格式 下载fastjson.jar 就行了

猜你喜欢

转载自blog.csdn.net/weixin_44647098/article/details/108502350