1

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?

shukhrat
  • 23
  • 5
  • Check this link https://stackoverflow.com/questions/63038456/how-to-model-a-profile-picture-type-of-field-in-user-entity-in-domain-driven-des – Archita Chaudhuri Aug 09 '22 at 06:22

1 Answers1

1

You could store and transmit the profile picture as a Base64 encoded string. Base64 is a well-known and well-supported binary-to-text encoding scheme. The downside is that it does increase the storage space requirement compared to binary by 33%.

I assume you have a client made in JavaScript. For encoding and decoding Base64 in JavaScript, see, for example, this StackOverflow answer: https://stackoverflow.com/a/247261/1333157

Browsers can even render Base64 encoded images just like that nowadays. It's a feature known as Data URIs; see, for example, here: https://css-tricks.com/data-uris/

ZeroOne
  • 3,041
  • 3
  • 31
  • 52