控制器:-
@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.]
1条答案
按热度按时间sr4lhrrt1#
这是控制器的外观**(以下代码有效):**
影片对象:
影片类型:
类型:
注:
POST请求示例:
确保以JSON格式发送请求主体:
祝好运