1

My Frontend is made with Angular. Backend is made with Java Spring.

I have two entities essen(meal) and essensplan (mealplan). Each essensplan consists of five objects of essen, configured as ManyToMany Relationship in Java Spring.

The Template displays every object of essen in a selectbox. With [ngValue]="essen" I pass the object essen to the method addEssenToEssensplan in my component and service.

essensplan-detail.component.html

<form *ngIf="essensplan">
 <div class="form-group">
  <select [(ngModel)]="essen" class="form-control" [ngModelOptions]="{standalone: true}">
    <option *ngFor="let essen of speisekarte" [ngValue]="essen" label="{{essen.name}}"></option>
    </select>
  </div>
    <button (click)="addEssenToEssensplan(essen)" type="button" class="btn btn- 
    primary">Hinzufügen</button>
</form>

essensplan-detail.component.ts

  addEssenToEssensplan(essen: Essen): void {
    this.essensplanService.addEssenToEssensplan(this.essensplan, essen);
  }

essensplan.service.ts

  addEssenToEssensplan(essensplan: Essensplan | number, essen: Essen): Observable<Essen> {
    const essensplanId = typeof essensplan === 'number' ? essensplan : essensplan.id;
    const currentEssen = essen;
    const url = `${this.essensplanUrl}/${essensplanId}/add`;
    const data = {id: essensplanId, essen: currentEssen};
    return this.http.post<Essen>(url, data);
  }

Controller in Java Spring

    @PostMapping(path = "/{id}/add")
    public void addEssenToEssensplan(@PathVariable("id") int id, @RequestBody Essen essen) {
        essensplanService.addEssenToEssensplan(id, essen);
    }

Service:

    @Transactional
    public Essensplan addEssenToEssensplan(int id, Essen essen) {

        Optional<Essensplan> optionalEssensplan = essensplanRepository.findById(id);
        if (optionalEssensplan.isPresent()) {
             Essensplan essensplan = optionalEssensplan.get();
            essensplan.getEssenProWoche().add(essen);
            return essensplan;
        } else return null;
    }

I already made a POST Request via Postman Client with for example http://localhost:8080/essensplan/2/add and it worked. For me it seems like the POST Request does not reach my Java Spring Controller.

1 Answer 1

1

The error is in your data object. You do not need to put essensplanId there. It is not part of the request body but a PathVariable.

Try :

    addEssenToEssensplan(essensplan: Essensplan | number, essen: Essen): Observable<Essen> {
        const essensplanId = typeof essensplan === 'number' ? essensplan : essensplan.id;
        return this.http.post<Essen>(`${this.essensplanUrl}/${essensplanId}/add`, {essen});
    }

Edit :

You also need to subscribe to your observable at some time or else the request will never be executed :

  addEssenToEssensplan(essen: Essen): void {
    this.essensplanService.addEssenToEssensplan(this.essensplan, essen).subscribe();
  }
Sign up to request clarification or add additional context in comments.

3 Comments

thanks, that made my code much cleaner. but sadly it still does not work. :(
There does not even appear an error. Not in the browser console and not in the console of the backend. The url for the post request should be fine tough. The method in my controller still does not get called.
I also console.log essen and the url. both are correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.