Upload image profile photo and associate to "profilePhoto" attribute in User entity with Spring

Asked

Viewed 21 times

-3

Guys I have an entity that way:

I am using Spring Boot and Postman to instantiate new User class objects

@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Entity
@Table(name = "tb_users")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @EqualsAndHashCode.Include
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cod_user")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "gender")
    private String gender;

    @Column(name = "birthday")
    private LocalDate birthday;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "cod_address", unique = true)
    private Address address;

    private ??? profilePhoto;

My question is: How to upload an image and associate it to the profilePhoto attribute, so that when I instantiate a new object I can insert this profile image in the user’s creation?

NOTE: I am not using Spring MVC or Thymeleaf, only Spring Boot and making the requests via Postman, in which case to create a new instance I am passing a JSON:

{
    "name": "Gui",
    "gender": "male",
    "birthday": "1998-12-12",
    "address":{
        "address": "Rua da Neves",
        "city": "Niterói",
        "state": "Rio de Janeiro",
        "postalCode": "579777-90",
        "country": "Brazil"
    },
    "profilePhoto": ???

How can I pass in my User Controller a way to create this image and associate to the profilePhoto attribute?

@PostMapping("/create")
@ResponseStatus(HttpStatus.CREATED)
public Person create(@RequestBody Person person) { 
    return personService.create(person); 
}

1 answer

0

A foto você envia através do form-data

The rest of your JSON vc sends in the body of the request normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.