我创建了rest api以使用Spring rest来保存学术大师,如下所示
@Secured("ROLE_ADMIN")
@RequestMapping(value = RestURIConstants.SAVE_ACADEMIC_YEAR, method = RequestMethod.POST)
public @ResponseBody AcademicYear saveAcademicYear(@RequestBody AcademicYear academicYear)
{
logger.info("saving academic year master");
academicYear = commonService.saveAcademicYear(academicYear);
return academicYear;
}
实体AcademicYear如下
@Entity
@Table(name = "COM_ACADEMIC_YEAR_MST")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "yearId")
public class AcademicYear implements java.io.Serializable
{
private static final long serialVersionUID = -8195008327271076910L;
@Id
@Column(name = "YEAR_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer yearId;
@Column(name = "YEAR_START_DT")
@Temporal(TemporalType.TIMESTAMP)
@JsonSerialize(using = DateSerializer.class)
@JsonDeserialize(using = DateDeserializer.class)
private Date yearStartDate;
@Column(name = "YEAR_END_DT")
@Temporal(TemporalType.TIMESTAMP)
@JsonSerialize(using = DateSerializer.class)
@JsonDeserialize(using = DateDeserializer.class)
private Date yearEndDate;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "academicYear", cascade = CascadeType.ALL)
List<CourseMaster> courseMaster;
@Column(name = "YEAR_STATUS")
Integer yearStatus;
@Column(name = "CREATED_DT")
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@Column(name = "CREATED_BY")
private String createdBy;
@Column(name = "UPDATED_DT")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDate;
@Column(name = "UPDATED_BY")
private String updatedBy;
@Column(name = "RECORD_STATUS")
private String recordStatus = "A";
public Integer getYearId()
{
return yearId;
}
public void setYearId(Integer yearId)
{
this.yearId = yearId;
}
public Date getYearStartDate()
{
return yearStartDate;
}
public void setYearStartDate(Date yearStartDate)
{
this.yearStartDate = yearStartDate;
}
public Date getYearEndDate()
{
return yearEndDate;
}
public void setYearEndDate(Date yearEndDate)
{
this.yearEndDate = yearEndDate;
}
public Integer getYearStatus()
{
return yearStatus;
}
public void setYearStatus(Integer yearStatus)
{
this.yearStatus = yearStatus;
}
public Date getCreatedDate()
{
return createdDate;
}
public void setCreatedDate(Date createdDate)
{
this.createdDate = createdDate;
}
public String getCreatedBy()
{
return createdBy;
}
public void setCreatedBy(String createdBy)
{
this.createdBy = createdBy;
}
public Date getUpdatedDate()
{
return updatedDate;
}
public void setUpdatedDate(Date updatedDate)
{
this.updatedDate = updatedDate;
}
public String getUpdatedBy()
{
return updatedBy;
}
public void setUpdatedBy(String updatedBy)
{
this.updatedBy = updatedBy;
}
public String getRecordStatus()
{
return recordStatus;
}
public void setRecordStatus(String recordStatus)
{
this.recordStatus = recordStatus;
}
public static long getSerialversionuid()
{
return serialVersionUID;
}
@Override
public String toString()
{
return "AcademicYear [yearId=" + yearId + ", yearStartDate=" + yearStartDate + ", yearEndDate=" + yearEndDate
+ ", yearStatus=" + yearStatus + ", createdDate=" + createdDate + ", createdBy=" + createdBy
+ ", updatedDate=" + updatedDate + ", updatedBy=" + updatedBy + ", recordStatus=" + recordStatus + "]";
}
public List<CourseMaster> getCourseMaster()
{
return courseMaster;
}
public void setCourseMaster(List<CourseMaster> courseMaster)
{
this.courseMaster = courseMaster;
}
}
但是当我尝试执行api时如下
{“yearStartDate”:“01-01-2015”},我发送标题为Content-Type:application / json
我正面临415:不支持的媒体类型错误
我找到了问题的根本原因,它位于以下行
OneToMany(fetch = FetchType.LAZY, mappedBy = "academicYear", cascade = CascadeType.ALL) List<CourseMaster> courseMaster;
如果我删除这一行就行了。