我无法从Web应用程序中添加对象,但是与邮递员一起可以正常工作

时间:2019-05-13 09:29:02

标签: java angular rest spring-boot java-ee

我正在尝试添加“组”,我正在使用Rest服务。我无法理解为什么在同一服务可以与Postman一起正常工作的同时出现http 400错误。

这是我在JEE中的代码

Groupe.java

@Entity
@Table(name = "groupes")
public class Groupe implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idgrp;



    @NotNull
   // @Lob
    private String nom_groupe;


    @ManyToOne(optional = false)
    @JoinColumn
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIgnore
    private EntityType entityType;

    public Long getIdgrp() {
        return idgrp;
    }

    public void setIdgrp(Long idgrp) {
        this.idgrp = idgrp;
    }

    public String getNom_groupe() {
        return nom_groupe;
    }

    public void setNom_groupe(String nom_groupe) {
        this.nom_groupe = nom_groupe;
    }

    public EntityType getEntityType() {
        return entityType;
    }

    public void setEntityType(EntityType entityType) {
        this.entityType = entityType;
    }



}

我的RestService

  @PostMapping("/entityTypes/{id_type}/groups")
    public Groupe createGroupe(@PathVariable (value = "id_type") Long id_type,
                                 @Valid @RequestBody Groupe groupe) {
        return entityTypeRepository.findById(id_type).map(entityType -> {
            groupe.setEntityType(entityType);
            return groupeRepository.save(groupe);
        }).orElseThrow(() -> new ResourceNotFoundException("id_type " + id_type + " not found", null, groupe));
    }

目前,我没有任何问题,当我尝试使用邮递员添加群组时,一切正常。我的问题是当我使用Angular

 onSubmit() {
this.grpInfo = new GroupeInfo(
      this.form.nom_groupe
    );
   headers.append('Authorization', window.localStorage.getItem('token'));
    this.http
      .post<any>('http://localhost:8080/entityTypes/' + this.route.snapshot.params['id'] + '/groups', { headers })
      .subscribe(
        (response) => {
         // console.log(response.content);
          response.content.nom_groupe = this.grpInfo.nom_groupe;
          //console.log(response);
        },
        (error) => {
          console.log(error);
        }
      );
 }

这是当我收到http 400错误以及后端出现此错误

[nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [1] in public com.hraccess.openhr.myEntities.Groupe com.hraccess.openhr.myController.GroupeController.createGroupe(java.lang.Long,com.hraccess.openhr.myEntities.Groupe): [Field error in object 'groupe' on field 'nom_groupe': rejected value [null]; codes [NotNull.groupe.nom_groupe,NotNull.nom_groupe,NotNull.java.lang.String,NotNull]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [groupe.nom_groupe,nom_groupe]; arguments []; default message [nom_groupe]]; default message [ne peut pas être nul]] ]

0 个答案:

没有答案