I am working on springboot project. I need to store user details along its profile picture in the backend project. here is my entity class:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "uuid")
private UUID uuid;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "middle_name", nullable = true)
private String middleName;
@Column(name = "employee_id", nullable = false)
private String employeeId;
@Column(name = "photo", nullable = true)
private String profilePicture;
@Column(name = "date_of_birth", nullable = false)
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
private Date dateOfBirth;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "phone_number", nullable = false)
private String phone;
@Column(name = "admin")
private Boolean admin;
}
Controller:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private ModelMapper modelMapper;
private UserService service;
UserController(UserService service){
this.service = service;
}
@GetMapping("/{uuid}")
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public UserDTO getUserById(@PathVariable UUID uuid) throws ResourceNotFoundException{
return service.getUserById(uuid);
}
@PostMapping("")
@ResponseStatus(HttpStatus.CREATED)
public User save(@RequestBody User user) throws ResourceAlreadyExistsException{
User user = dtoToEntity(userDTO);
return service.saveUser(user);
}
}
I checked Multipartfile to handle to save files. But i had to create separate controller and service class to store a file. Is there any way to recieve profile picture with user body and save it, also retrieve it when using GET method?