控制器:-
@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.IllegalArgumentException:根本原因没有枚举常量com.example.Model.MovieType。答案 0 :(得分:0)
这是您的控制器的外观 (以下代码有效):
@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;
}
}
MovieType:
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;
}
}
注释:
示例POST请求:
{
"name": "name",
"releasedate": "2018-12-23T10:01:47.056+0000",
"type": "English",
"movie_length": 1,
"genre": "Action"
}
祝你好运