I'm developing a Spring Boot application and I have a problem with my POST request. When I do the request it seems that every field in the request body is null.
Client entity
package com.tradeManagementApp.tradeManagement.model;
import lombok.*;
import javax.persistence.*;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Table(name ="Client")
@Entity
public class Client extends AdstractEntity{
@Column(name = "nom")
private String nom;
@Column(name = "prenom")
private String prenom;
@Column(name = "photo")
private String photo;
@Embedded
private Adresse adresse;
@Column(name = "mail")
private String mail;
@Column(name = "tel")
private String tel;
@Column(name = "identreprise")
private int idEntreprise;
@OneToMany(mappedBy = "client")
private List<CommandeClient> commandeClients;
}
Adress entity
package com.tradeManagementApp.tradeManagement.model;
import lombok.*;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class Adresse implements Serializable {
@Column(name = "adresee1")
private String adresse1;
@Column(name = "adresee2")
private String adresse2;
@Column(name = "ville")
private String ville;
@Column(name = "pays")
private String pays;
@Column(name = "codepostal")
private String codepostal;
}
Client DTO
@Builder
@Data
public class ClientDto {
private Integer id;
private String nom;
private String prenom;
private String photo;
private AdresseDto adresse;
private String mail;
private Integer idEntreprise;
private String tel;
@JsonIgnore
private List<CommandeClientDto> commandeClients;
public static ClientDto fromEntity (Client client){
if (client == null){
// todo throw an exception
return null;
}
return ClientDto.builder()
.id(client.getId())
.nom(client.getNom())
.prenom(client.getPrenom())
.photo(client.getPhoto())
.adresse(AdresseDto.fromEntity(client.getAdresse()))
.mail(client.getMail())
.idEntreprise(client.getIdEntreprise())
.tel(client.getTel())
.build();
}
public static Client toEntity(ClientDto clientDto){
if (clientDto == null){
// todo throw an exception
return null;
}
Client client = new Client();
client.setId(clientDto.getId());
client.setNom(clientDto.getNom());
client.setPrenom(clientDto.getPrenom());
client.setPhoto(clientDto.getPhoto());
client.setMail(clientDto.getMail());
client.setTel(clientDto.getTel());
client.setIdEntreprise(clientDto.getIdEntreprise());
client.setAdresse(AdresseDto.toEntity(clientDto.getAdresse()));
return client;
}
}
Adress DTO
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class AdresseDto {
private String adresse1;
private String adresse2;
private String ville;
private String pays;
private String codePostal;
public static AdresseDto fromEntity(Adresse adresse){
if (adresse == null){
// todo throw an exception
return null;
}
return AdresseDto.builder()
.adresse1(adresse.getAdresse1())
.adresse2(adresse.getAdresse2())
.ville(adresse.getVille())
.pays(adresse.getPays())
.codePostal(adresse.getCodepostal())
.build();
}
public static Adresse toEntity (AdresseDto adresseDto){
if (adresseDto == null){
// todo throw an exception
return null;
}
Adresse adresse = new Adresse();
adresse.setAdresse1(adresseDto.getAdresse1());
adresse.setAdresse2(adresseDto.getAdresse2());
adresse.setVille(adresseDto.getVille());
adresse.setPays(adresseDto.getPays());
adresse.setCodepostal(adresseDto.getCodePostal());
return adresse;
}
}
client validator
public class ClientValidator {
public static List<String> validate (ClientDto clientDto){
List<String> errors = new ArrayList<>();
if (clientDto == null){
errors.add("veuillez renseignez le nom du client");
errors.add("veuillez renseignez le prenom du client");
errors.add("veuillez renseignez l'email du client");
return errors;
}
if ( !StringUtils.hasLength(clientDto.getNom())){
errors.add("veuillez renseignez le nom du client");
}
if ( !StringUtils.hasLength(clientDto.getPrenom())){
errors.add("veuillez renseignez le prenom du client");
}
if ( !StringUtils.hasLength(clientDto.getMail())){
errors.add("veuillez renseignez l'email du client");
}
if ( !StringUtils.hasLength(clientDto.getTel())){
errors.add("veuillez renseignez le numero de telephone du client");
}
return errors;
}
}
Save client method in Service
@Override
public ClientDto save(ClientDto clientDto) {
List<String> errors = ClientValidator.validate(clientDto);
if (!errors.isEmpty()){
log.error("Client is not Valid {}",clientDto);
throw new InvalidEntityException("le client est invalide", ErrorCode.CLIENT_NOT_VALID, errors);
}
return ClientDto.fromEntity(
clientRepositry.save(ClientDto.toEntity(clientDto))
);
}
Client controller
@PostMapping(value = CLIENT_ENDPOIND+"/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "enregistrer un client", description = "cette methode permet de enregistrer un client"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "client enregistrer avec succée"),
@ApiResponse(responseCode = "400", description = "les informations du clients ne sont pas valides")
})
ClientDto save(@RequestBody ClientDto clientDto);
When i do a post request using swagger UI
curl -X 'POST' \
'http://localhost:8080/tradeManagement/v1/clients/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"nom": "string",
"prenom": "string",
"photo": "string",
"adresse": {
"adresse1": "string",
"adresse2": "string",
"ville": "string",
"pays": "string",
"codePostal": "string"
},
"mail": "string",
"idEntreprise": 0,
"tel": "string"
}'
I have an error stating that the values of all attributes of customer Dto are null.
ERROR 13948 --- [nio-8080-exec-6] c.t.t.s.i.ClientServiceImplementation : Client is not Valid ClientDto(id=null, nom=null, prenom=null, photo=null, adresse=null, mail=null, idEntreprise=null, tel=null, commandeClients=null)
response body
{
"httpCode": 400,
"errorCode": "CLIENT_NOT_VALID",
"message": "le client est invalide",
"errors": [
"veuillez renseignez le nom du client",
"veuillez renseignez le prenom du client",
"veuillez renseignez l'email du client",
"veuillez renseignez le numero de telephone du client"
]
}
You may have incorrect import in your controller:
Add:
import org.springframework.web.bind.annotation.RequestBody;
Remove:
import io.swagger.v3.oas.annotations.parameters.RequestBody;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With