我想发送@RequestBody
作为邮政请求到宁静的服务。在@RequestBody
我有id
,title
,aboutMe
和image file
。我设置produces = MediaType.MULTIPART_FORM_DATA_VALUE
but当我检查休息我得到错误,说406不能接受。我怎么解决它?
我的控制器:
@RequestMapping(value = "aboutMe", method = RequestMethod.POST, produces = MediaType.MULTIPART_FORM_DATA_VALUE)
public String saveAboutMe(@RequestBody Author author) {
authorService.saveAboutMe(author);
return "saved";
}
和实体
@Entity
@Table(name = "author")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HIBERNATE_SEQUENCE")
@SequenceGenerator(name = "HIBERNATE_SEQUENCE", sequenceName = "HIBERNATE_SEQUENCE", allocationSize = 1, initialValue = 1)
private int id;
@Column(name = "title")
private String title;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
@Column(name = "dateOfAuthor")
@Temporal(TemporalType.TIMESTAMP)
private Date modifiedTime;
@Column(name = "aboutMe", length = 10000)
private String aboutMe;
@Column(name = "image")
@Lob
private Blob image;
}
我从休息error得到的截图
你无法传递@RequestBody和Blob对象,图像应该是MultipartFile对象。所以对于工作代码:
1)将SessionFactory注入控制器类或要将Mulitpart转换为Blob对象的任何类中
@Autowired
private SessionFactory sessionFactory;
2)Controller类的更改:
@RequestMapping(value = "aboutMe", method = RequestMethod.POST)
public String saveAboutMe(Author author,@RequestPart("image") MultipartFile imageFile) {
//Convert Multipart file into BLOB
try {
Blob image = Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(authorImage.getInputStream(),authorImage.getSize());
author.setImage(image);
} catch (HibernateException | IOException exception) {
log.error(exception.getMessage(),exception);
}
authorService.saveAboutMe(author);
return "saved";
}