java—如何将jsonMap到springboot中包含对象列表的对象

nhaq1z21  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(399)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

上个月关门了。
改进这个问题
edit:正如madlemon指出的,请求json是不正确的。
我在做一个爱好项目,我试图创建一个电影评论网站。我有以下模型课程:
电影类

public class Movie {
    private String movieId;
    private String name;
    private LocalDate releaseDate;
    private String director;
    private List<Actor> cast;
    private String synopsys;
    private List<Genre> genre;
    private String poster;
    private String trailer;
    private int length;
    private Rating rating;
    private Language language;
    private Country country;
    ...
}

演员级

public class Actor {
    private String actorId;
    private String name;
    private String image;
    private LocalDate dateOfBirth;
    private Country country;
    private Gender gender;
    private int movieCount;
    ...
}

我想从 swagger 传过来

{
    "cast": [
      {
        "actorId": "A61186",
        "name": "Leonardo DiCaprio",
        "image": "https://upload.wikimedia.org/wikipedia/commons/4/46/Leonardo_Dicaprio_Cannes_2019.jpg",
        "dateOfBirth": "1974-11-11",
        "country": "US",
        "gender": "MALE",
        "movieCount": 1
      },
      {
        "actorId": "A39986",
        "name": "Cillian Murphy",
        "image": "http://www.gstatic.com/tv/thumb/persons/236083/236083_v9_bb.jpg",
        "dateOfBirth": "1976-05-25",
        "country": "UK",
        "gender": "MALE",
        "movieCount": 1
      }
    ],
    "movieId": "M80284",
    "name": "Inception",
    "releaseDate": "2010-07-16",
    "director": "Christopher Nolan",
    "cast": null,
    "synopsys": "Cobb steals information from his targets by entering their dreams. Saito offers to wipe clean Cobb's criminal history as payment for performing an inception on his sick competitor's son.",
    "genre": [
      "ACTION",
      "FICTION"
    ],
    "poster": "https://miro.medium.com/max/800/1*Aop38tMyxWjGowYDVX4Nqw@2x.jpeg",
    "trailer": "https://www.youtube.com/watch?v=YoHD9XEInc0",
    "length": 162,
    "rating": "PG_13",
    "language": "EN",
    "country": "US"
  }

但是它抛出了空指针异常,并且当我检查日志时,它似乎没有Map到参与者列表。
从日志

2021-03-01 14:49:27.094  INFO 14612 --- [nio-8181-exec-4] c.k.m.portal.services.MoviesServiceImpl  : Editing movie with id : M80284
2021-03-01 14:49:27.094  INFO 14612 --- [nio-8181-exec-4] c.k.m.portal.services.MoviesServiceImpl  : Updating with new details : Movie{movieId='M80284', name='Inception', cast=null, releaseDate=2010-07-16, director='Christopher Nolan', synopsys='Cobb steals information from his targets by entering their dreams. Saito offers to wipe clean Cobb's criminal history as payment for performing an inception on his sick competitor's son.', genre=[ACTION, FICTION], poster='https://miro.medium.com/max/800/1*Aop38tMyxWjGowYDVX4Nqw@2x.jpeg', trailer='https://www.youtube.com/watch?v=YoHD9XEInc0', length=162, rating=PG_13, language=EN, country=US}
Hibernate: select moviesenti0_.movie_id as movie_id1_1_0_, moviesenti0_.cast_actor as cast_act2_1_0_, moviesenti0_.country as country3_1_0_, moviesenti0_.director as director4_1_0_, moviesenti0_.genre as genre5_1_0_, moviesenti0_.language as language6_1_0_, moviesenti0_.length as length7_1_0_, moviesenti0_.name as name8_1_0_, moviesenti0_.poster as poster9_1_0_, moviesenti0_.rating as rating10_1_0_, moviesenti0_.release_date as release11_1_0_, moviesenti0_.synopsys as synopsy12_1_0_, moviesenti0_.trailer as trailer13_1_0_ from movie moviesenti0_ where moviesenti0_.movie_id=?
2021-03-01 14:49:27.096 ERROR 14612 --- [nio-8181-exec-4] c.k.m.portal.utilities.LoggingUtility    : null

我怎样才能把它画出来?
这是我的方法

@PutMapping("/{movieId}")
    @ApiOperation("PUT method to update existing movie")
    @ApiResponse(code = 201, message = "Movie updated successfully")
    public ResponseEntity<Movie> updateMovie(@PathVariable String movieId, @RequestBody Movie movie) {
        Movie res = moviesService.editMovie(movieId, movie);
        return new ResponseEntity<>(res, HttpStatus.CREATED);
    }
h9vpoimq

h9vpoimq1#

"cast": [...],
...
"cast": null,

您的json包含 cast 两次。一次 null 作为值。

hgc7kmma

hgc7kmma2#

我确实有许多列表和集合反序列化问题,我通过重写反序列化程序或使用dto解决了这些问题。就我个人而言,我更喜欢DTO。

相关问题