我使用Angular 4作为前端,Spring作为后端,我想在我的数据库中存储一个包含图像的用户。据我所知,Blob类型在Angular 4中存在,但在Spring中没有;byte类型在Spring中存在,但在Angular 4中不存在。如何使用REST API将图像从前端传输到后端。
这是我的实体:
import java.sql.Blob;
@Entity
@Table(name = "USER_OAUTH", schema = "OAUTH")
public class UserOauth implements java.io.Serializable {
private long idUser;
private String username;
private String firstName;
private String lastName;
private String adrEmail;
private int isAdAccount;
private Date lastConnection;
private String password;
private boolean enabled;
private int attempts;
private Blob photo;
@Lob
@Column(name = "PHOTO", length = 10000000)
public Blob getPhoto() {
return photo;
}
这是我的DTO:
public class UserDTO implements java.io.Serializable {
private long idUser;
private String username;
private String firstName;
private String lastName;
private String adrEmail;
private int isAdAccount;
private Date lastConnection;
private String password;
private boolean enabled;
private Blob photo;
这是我的web服务:(在添加照片之前它可以正常工作):
// Add a new user;
@RequestMapping(value = "/users", method = RequestMethod.POST)
public UserDTO addUser(@RequestBody final UserDTO user) throws Exception {
return modelMapper.map(userService.saveUser(modelMapper.map(user, UserOauth.class)), UserDTO.class);
}
最后是我的打字稿:
export class UserData {
public idUser: number;
public username: string;
public firstName: string;
public lastName: string;
public adrEmail: string;
public isAdAccount: boolean;
public lastConnection: Date;
public password: string;
public enabled: boolean;
public photo:Blob;
}
如果有任何整改,请让我知道。提前谢谢你
发布于 2018-06-07 19:17:02
发布于 2018-06-07 18:49:15
在TypeScript/ Angular中,你可以像这样声明你的字段:img: File;
Spring中的RestController中的DTO应该使用private final byte[] img;
,最后还应该使用实体
@Lob
@Column(name = "IMG", length = 10000000)
private byte[] img;
https://stackoverflow.com/questions/50738477
复制相似问题