在REST控制器中传递枚举时,我得到了java.lang.IllegalArgumentException,而我使用的是枚举作为字符串

uqdfh47h  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(97)

控制器:-

@RequestMapping(value="/CreateMovie", method= RequestMethod.POST)
    public HttpStatus createMovie(@RequestBody JSONObject object) {
        Movie movie =new Movie(object.optString("name"),
                    MovieType.valueOf(object.optString("type")),
                    new Date(object.optString("releasedate")),
                    Integer.parseInt(object.optString("time")), 
                    Genre.valueOf(object.optString("genre")));
            //object.

    repository.save(movie);

    return HttpStatus.OK;

}

实体如下:

@Entity(name="Movie")
public class Movie {

    public Movie()
    {

    }
    public Movie(String name,MovieType type,Date releasedate,
          int movie_length, Genre genre)
    {
        this.name=name;
        this.type=type;
        this.releasedate=releasedate;
        this.movie_length=movie_length;
        this.genre=genre;
    }

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="Id")
    private long id;

    @Column(name="movie_name")
    @NotNull
    String name;
    @Enumerated(EnumType.STRING)
    //@NotNull
    MovieType type;
    @Column(name="release_date")
    @JsonFormat(shape=STRING, pattern="dd-MM-yyyy")
    //@NotNull
    Date releasedate;
    @Column(name="movie_length")
    //@NotNull
    int movie_length;
    @Column(name="like_percentage")
    float likepercentage;
    @Column(name="no_of_reactions")
    float no_of_reactions;
    @Column(name="avg_rating")
    float avg_rating;
    @Column(name="no_of_reviews")
    int no_of_reviews;
    @Column(name="director")
    @NotNull
    String director;
    @Transient
    List<String> cities;
    @Transient
    List<String> cast;

枚举如下所示:-

public enum MovieType {

    Hindi("Hindi"),
    English("English"),
    Telugu("Telugu"),
    Malyalam("Malyalam"),
    Kannada("Kannnada");

    private String name;

    public String getName() {
        return name;
    }

    MovieType(String name) {
        this.name = name;
    }

}

堆栈跟踪如下所示:
路径为[]的上下文中的servlet [dispatcherServlet]的Servlet. service()引发异常[请求处理失败;嵌套异常是java. lang.没有具有根本原因的枚举常量com.example.Model.MovieType.]

sr4lhrrt

sr4lhrrt1#

这是控制器的外观**(以下代码有效):**

@GetMapping(path = "/movies")
public Movie getMovie() {
    Movie movie = new Movie("name", MovieType.English, new Date(), 1, Genre.Action);
    return movie;
}

@PostMapping(path = "/movies")
public HttpStatus createMovie(@RequestBody Movie movie) {
    repository.save(movie);
    return HttpStatus.OK;
}

影片对象:

public class Movie {

    public String name;
    public Date releasedate;
    public MovieType type;
    public int movie_length;
    public Genre genre;

    public Movie() {}

    public Movie(String name, MovieType type, Date releasedate, int movie_length, Genre genre) {
        this.name = name;
        this.type = type;
        this.releasedate = releasedate;
        this.movie_length = movie_length;
        this.genre = genre;
    }
}

影片类型:

public enum MovieType {
    Hindi("Hindi"), English("English"), Telugu("Telugu"), Malyalam("Malyalam"), Kannada("Kannnada");

    private String name;

    public String getName() {
        return name;
    }

    MovieType(String name) {
        this.name = name;
    }
}

类型:

public enum Genre {
    Daram("Drama"), Action("Action");

    private String name;

    public String getName() {
        return name;
    }

    Genre(String name) {
        this.name = name;
    }
}

注:

  • 将Movie作为对象传递-不要使用JsonObject解析json
  • 使用如上所述的url约定(即电影同时用于GET和POST)
  • 使用getMovie()获取电影对象,并使用其模板通过POST将类似对象发送到createMovie()
  • 使用Postman或任何其他Rest客户端测试代码
    POST请求示例:
{
    "name": "name",
    "releasedate": "2018-12-23T10:01:47.056+0000",
    "type": "English",
    "movie_length": 1,
    "genre": "Action"
}

确保以JSON格式发送请求主体:

祝好运

相关问题