pip*_*lam 5 java spring spring-boot localdate
嘿,我遇到了与这里相同的问题:JSON Java 8 LocalDateTime format in Spring Boot我尝试了那里的解决方案,但它不起作用。有人可以告诉我我做错了什么吗?
\n\n我添加了
\n\nspring.jackson.serialization.write-dates-as-timestamps=false\nRun Code Online (Sandbox Code Playgroud)\n\n到 application.property\n我的模型类如下所示:
\n\npackage bookrental.model.book;\n\nimport bookrental.model.account.User;\nimport com.fasterxml.jackson.annotation.JsonFormat;\nimport com.fasterxml.jackson.databind.annotation.JsonDeserialize;\nimport com.fasterxml.jackson.databind.annotation.JsonSerialize;\nimport com.fasterxml.jackson.databind.util.ISO8601DateFormat;\nimport com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;\nimport com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;\nimport lombok.*;\n\nimport javax.persistence.*;\nimport java.time.LocalDateTime;\nimport java.util.Date;\n\n@Entity\n@Getter\n@Setter\n@EqualsAndHashCode\n@AllArgsConstructor\n@NoArgsConstructor\npublic class BookRentals {\n\n @Id\n @GeneratedValue(strategy = GenerationType.AUTO)\n private int id;\n @OneToOne\n private Book book;\n @OneToOne\n private User user;\n @JsonFormat(pattern = ("yyyy/MM/dd HH:mm:ss"))\n @JsonSerialize(using = LocalDateTimeSerializer.class)\n @JsonDeserialize(using = LocalDateTimeDeserializer.class)\n private LocalDateTime dateOfRental;\n\n public BookRentals(Book book, User user) {\n this.book = book;\n this.user = user;\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我这样设定时间:
\n\nprivate BookRentals prepareBookToRent(int userID, Book book) {\n BookRentals bookRentals = new BookRentals(book, new User(userID));\n bookRentals.setDateOfRental(LocalDateTime.now());\n return bookRentals;\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我添加了依赖:
\n\n<dependency>\n <groupId>com.fasterxml.jackson.datatype</groupId>\n <artifactId>jackson-datatype-jsr310</artifactId>\n <version>2.9.7</version>\n </dependency>\nRun Code Online (Sandbox Code Playgroud)\n\n我的 JSON 看起来像这样:
\n\n[\n {\n "book": {\n "author": "Henryk Sienkiewicz",\n "category": "powie\xc5\x9b\xc4\x87 historyczna",\n "id": 1,\n "title": "Krzy\xc5\xbcacy"\n },\n "class": "bookrental.model.book.BookRentals",\n "dateOfRental": {\n "class": "java.time.LocalDateTime",\n "dayOfMonth": 19,\n "dayOfWeek": "WEDNESDAY",\n "dayOfYear": 353,\n "hour": 0,\n "minute": 13,\n "month": "DECEMBER",\n "monthValue": 12,\n "nano": 758649300,\n "second": 8,\n "year": 2018\n },\n "id": 1,\n "user": {\n "id": 2,\n "name": "piotri",\n "password": "123"\n }\n }\n]\nRun Code Online (Sandbox Code Playgroud)\n\n我还应该做什么?
\n\n我没有尝试使用类的解决方案,因为我不知道应该将它们放在哪个包中。\n//编辑\n经过 Erik\ 的建议,pom.xml 如下所示:
\n\n<?xml version="1.0" encoding="UTF-8"?>\n<project xmlns="http://maven.apache.org/POM/4.0.0"\n xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\n<modelVersion>4.0.0</modelVersion>\n\n<groupId>com.book.rental.piotrek</groupId>\n<artifactId>BookRental</artifactId>\n<version>1.0-SNAPSHOT</version>\n\n<build>\n <plugins>\n <plugin>\n <groupId>org.apache.maven.plugins</groupId>\n <artifactId>maven-compiler-plugin</artifactId>\n <configuration>\n <source>8</source>\n <target>8</target>\n </configuration>\n </plugin>\n </plugins>\n</build>\n\n<parent>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-parent</artifactId>\n <version>2.1.1.RELEASE</version>\n <relativePath/>\n</parent>\n\n\n<dependencies>\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-web</artifactId>\n </dependency>\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-test</artifactId>\n <scope>test</scope>\n </dependency>\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-data-jpa</artifactId>\n </dependency>\n <dependency>\n <groupId>com.h2database</groupId>\n <artifactId>h2</artifactId>\n </dependency>\n <dependency>\n <groupId>org.projectlombok</groupId>\n <artifactId>lombok</artifactId>\n <version>1.16.22</version>\n </dependency>\n <dependency>\n <groupId>javax.validation</groupId>\n <artifactId>validation-api</artifactId>\n </dependency>\n <dependency>\n <groupId>net.sf.flexjson</groupId>\n <artifactId>flexjson</artifactId>\n <version>2.1</version>\n </dependency>\n</dependencies>\n\n</project>\nRun Code Online (Sandbox Code Playgroud)\n\n升级失败。\nJSON:
\n\n[\n {\n "book": {\n "author": "Henryk Sienkiewicz",\n "category": "powie\xc5\x9b\xc4\x87 historyczna",\n "id": 1,\n "title": "Krzy\xc5\xbcacy"\n },\n "dateOfRental": {\n "dayOfMonth": 19,\n "dayOfWeek": "WEDNESDAY",\n "dayOfYear": 353,\n "hour": 11,\n "minute": 22,\n "month": "DECEMBER",\n "monthValue": 12,\n "nano": 884499000,\n "second": 17,\n "year": 2018\n },\n "id": 7,\n "user": {\n "id": 5,\n "name": "admin",\n "password": "123"\n }\n }\n]\nRun Code Online (Sandbox Code Playgroud)\n\n图书租赁:
\n\npackage bookrental.model.book;\n\nimport bookrental.model.account.User;\nimport lombok.*;\n\nimport javax.persistence.*;\nimport java.time.LocalDateTime;\n\n@Entity\n@Getter\n@Setter\n@EqualsAndHashCode\n@AllArgsConstructor\n@NoArgsConstructor\npublic class BookRentals {\n\n @Id\n @GeneratedValue(strategy = GenerationType.AUTO)\n private int id;\n @OneToOne\n private Book book;\n @OneToOne\n private User user;\n private LocalDateTime dateOfRental;\n\n public BookRentals(Book book, User user) {\n this.book = book;\n this.user = user;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n//编辑2
\n\n嘿。无意中我发现了问题的原因。我有一个类负责为确切的用户找到确切的咆哮。当我去时,/books/rentals/{userID}我得到了格式正确的日期。正如你所看到的方法 return List<BookRentals>。在BookRentalsService我返回 ResponseEntity 中,我认为因此它看起来像这样。你知道如何解决吗?
package bookrental.service.account;\n\n import bookrental.model.book.BookRentals;\n import bookrental.repository.book.BookRentalsRepository;\n import org.springframework.beans.factory.annotation.Autowired;\n import org.springframework.stereotype.Service;\n\n import java.util.List;\n\n @Service\n public class UserRentalsService {\n\n private final BookRentalsRepository bookRentalsRepository;\n\n @Autowired\n public UserRentalsService(BookRentalsRepository bookRentalsRepository) {\n this.bookRentalsRepository = bookRentalsRepository;\n }\n\n public List<BookRentals> findUserRentalsByGivenID(int userID) {\n return bookRentalsRepository.getUserRentalsByGivenID(userID);\n }\n }\n\n\npackage bookrental.controller.account;\n\nimport bookrental.model.book.BookRentals;\nimport bookrental.service.account.UserRentalsService;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.PathVariable;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport java.util.List;\n\n@RestController\npublic class UserRentalsController {\n\n private final UserRentalsService userRentalsService;\n\n @Autowired\n public UserRentalsController(UserRentalsService userRentalsService) {\n this.userRentalsService = userRentalsService;\n }\n\n @GetMapping("books/rentals/{userID}")\n public List<BookRentals> findUserRentalsByGivenID(@PathVariable int userID) {\n return userRentalsService.findUserRentalsByGivenID(userID);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n图书租赁服务
\n\npackage bookrental.service.book.rentals;\n\nimport bookrental.model.account.User;\nimport bookrental.model.book.Book;\nimport bookrental.model.book.BookRentals;\nimport bookrental.repository.account.UserRepository;\nimport bookrental.repository.book.BookRepository;\nimport bookrental.repository.book.BookRentalsRepository;\nimport flexjson.JSONSerializer;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.http.HttpHeaders;\nimport org.springframework.http.HttpStatus;\nimport org.springframework.http.ResponseEntity;\nimport org.springframework.stereotype.Service;\n\nimport java.time.LocalDateTime;\nimport java.util.ArrayList;\nimport java.util.List;\n\n@Service\npublic class BookRentalService {\n\n private final UserRepository userRepository;\n private final BookRepository bookRepository;\n private final BookRentalsRepository bookRentalsRepository;\n\n @Autowired\n public BookRentalService(BookRepository bookRepository, BookRentalsRepository bookRentalsRepository, UserRepository userRepository) {\n this.bookRepository = bookRepository;\n this.bookRentalsRepository = bookRentalsRepository;\n this.userRepository = userRepository;\n }\n\n ....\n\n public ResponseEntity<String> findAllRentals() {\n List<BookRentals> rentedBooks = new ArrayList<>();\n bookRentalsRepository.findAll().forEach(rentedBooks::add);\n HttpHeaders headers = new HttpHeaders();\n headers.add("Content-Type", "application/json; charset=utf-8");\n return new ResponseEntity<>(new JSONSerializer().exclude("book.class")\n .exclude("book.available")\n .exclude("dateOfReturn")\n .exclude("*.class")\n .exclude("user.amountOfCashToPay")\n .exclude("password")\n .serialize(rentedBooks), headers, HttpStatus.OK);\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
您甚至需要@JsonSerialize(using = LocalDateTimeSerializer.class)和吗@JsonDeserialize(using = LocalDateTimeDeserializer.class)?
我遇到了完全相同的问题,也使用了jackson-datatype-jsr310依赖项。切换到spring-boot-starter-json解决了我的问题:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10842 次 |
| 最近记录: |