1
votes

i am new with angular 6 . i have developed a post service with spring boot and when i test it via postman ,it works perfectly but when i test it with web browser it gave me this error :

HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "OK", url: "http://localhost:8080/api/test/ordermiss", ok: false, …}error: {timestamp: "2019-07-10T13:35:09.083+0000", message: null, details: "uri=/api/test/ordermiss"}details: "uri=/api/test/ordermiss"message: nulltimestamp: "2019-07-10T13:35:09.083+0000"proto: Objectheaders: HttpHeaderslazyInit: ƒ ()lazyUpdate: nullnormalizedNames: Map(0) {}proto: Objectmessage: "Http failure response for http://localhost:8080/api/test/ordermiss: 500 OK"name: "HttpErrorResponse"ok: falsestatus: 500statusText: "OK"url: "http://localhost:8080/api/test/ordermiss"proto: HttpResponseBaseconstructor: ƒ HttpErrorResponse(init)proto: Object

and when i take a look to the spring console it gave this warning :

2019-07-11 13:36:01.796 WARN 14404 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.NullPointerException]

i worked with the same method with another object but i didn't get this error so please could you help me

this the service :

                constructor(private http: HttpClient) { }

           SaveOM(Om: Object): Observable<Object> {
         return this.http.post(`${this.baseUrl}`, Om);
           }

and this is the .ts :

    save() {
      this.omService.SaveOM(this.ordermission)
      .subscribe(data => console.log(data), error => console.log(error));
    this.ordermission = new Ordermission();
      }

     onSubmit() {
       this.submitted = true;
          this.save();
       }

the class angular:

           export class Ordermission {
          id:number;
        mat:string;
         depart:string;
         dest:string;
      // etat:string;enum class
       // type:string;enum class
        // localisation:string;enum class
         datedeb:Date;
        datefin:Date;
         distance:number;
         couttranp:number;
         coutheberg:number;
       total:number;
        }

the controller:

              @CrossOrigin(origins = "*", maxAge = 3600)
             @RestController
            @RequestMapping("/api/test")
              @PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
          public class OrdermissionController {

  @Autowired private OrdermissionRepository ordermissionrep;

  @Autowired private OrderMissService orderMissService;


  @PostMapping("/ordermiss")
// @ExceptionHandler(RuntimeException.class)
    public Ordermission createOM(@Valid @RequestBody Ordermission OM) {
        return orderMissService.saveOM(OM);

    }

the service :

                 @Service
                public class OrderMissServiceImpl implementsOrderMissService {
                 Double var;
                 Double tot;
                 long cal;
                long cout;

  @Autowired private OrdermissionRepository ordermissionrep;


@Override
public Ordermission saveOM(Ordermission om) {
        if(om.getDepart().equals(Local.tunis) && om.getDest().equals(Local.sousse) || om.getDepart().equals(Local.sousse) && om.getDest().equals(Local.tunis)) {
        var =(double) 143;
        om.setDistance(var);
    }
    if(om.getDepart().equals(Local.tunis) && om.getDest().equals(Local.tabarka) || om.getDepart().equals(Local.tabarka) && om.getDest().equals(Local.tunis)) {
        var =(double) 175;
        om.setDistance(var);
    }


    if(om.getDepart().equals(Local.tunis) && om.getDest().equals(Local.zaghouan) || om.getDepart().equals(Local.zaghouan) && om.getDest().equals(Local.tunis)) {
        var =(double) 57;
        om.setDistance(var);
    }
    if(om.getDepart().equals(Local.tunis) && om.getDest().equals(Local.zarsis) || om.getDepart().equals(Local.zarsis) && om.getDest().equals(Local.tunis)) {
        var =(double) 544;
        om.setDistance(var);
    }

      if(om.getType().equals(Typetransport.perso)) {
          cal=(long) (om.getDistance()*2*0.08);
          om.setCouttranp(cal);
      }

      if(om.getType().equals(Typetransport.pubic)) {
          cal=(long) (om.getDistance()*2*0.375);
          om.setCouttranp(cal);
      }

     if(om.getEtat().equals(Etat.cadre)) {
         cout=(DaysBetween(om)+1)*25;
         om.setCoutheberg(cout);
     }
     if(om.getEtat().equals(Etat.non_cadre)) {
         cout=(DaysBetween(om)+1)*20;
         om.setCoutheberg(cout);
     }
     else if (om.getCoutheberg() != null && om.getCouttranp()!=null) {
        tot = (double) (om.getCoutheberg()+om.getCouttranp());
        om.setTotal(tot);
    }
    return  ordermissionrep.save(om);

}

The entity :

      @Entity
    @Table(name = "ordermission")
   public class Ordermission {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private String mat;

@Enumerated(EnumType.STRING)
private Typetransport type;

@Enumerated(EnumType.STRING)
private Etat etat;

@Enumerated(EnumType.STRING)
private Local depart;

@Enumerated(EnumType.STRING)
private Local dest;
@Temporal(TemporalType.DATE)
private Date datedeb;
@Temporal(TemporalType.DATE)
private Date datefin;

private Double distance;

private Long couttranp;
private Long coutheberg;

     // @Formula("couttranp + coutheberg")
       private Double total;  
1
500 is a Server Error. please check your server if its working fine. or recheck your parameters you are sending. - Najam Us Saqib
Hello rania, share your controller - Romil Patel
i edit it @PatelRomil (the controller an the service) - rania
okay from the code what I can tell is one of the attribute in Ordermission seems to be null, can you add the Request body - Avi
i added the body @Avi but why it works with postman and when it pass with angular it gave me this error - rania

1 Answers

0
votes

error 500 (intrenal server error) sometimes show CORS problem. Check your server is properly configured to accept CORS request