1

I know there are a few questions on stackoverflow regarding this problem. But I have have been spending hours trying to resolve this error without any success.

I am using the mysql database to store the values.

I keep on getting the error message from the com.example.springboot.Recipe file.

This is springboot recipe file

package com.example.springboot;
import com.example.springboot.Recipe;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Entity // This tells Hibernate to make a table out of this class
public class Recipe {
 public Recipe(){
 }
 public Recipe(Integer id, String name, String description, String type, Integer preptime, Integer cooktime, String content, Integer difficulty){
 this.id = id;
 this.name = name;
 this.description = description;
 this.type = type;
 this.preptime = preptimee;
 this.cooktime = cooktime;
 this.content = content;
 this.difficulty = difficulty;
 }
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 private Integer id;
 private String name;
 private String description;
 private String type;
 private Integer preptime;
 private Integer cooktime;
 @Column(columnDefinition = "TEXT")
 private String content;
 private Integer difficulty;
 public Integer getId() {
 return id;
 }
 public void setId(Integer id) {
 this.id = id;
 }
 public String getTitle() {
 return name;
 }
 public void setTitle(String name) {
 this.name = name;
 }
 public String getDescription() {
 return description;
 }
 public void setDescription(String description) {
 this.description = description;
 }
 public String getType() {
 return type;
 }
 public void setType(String type) {
 this.type = type;
 }
 public String getContent() {
 return content;
 }
 public void setContent(String content) {
 this.content = content;
 }
 public Integer getDifficulty() {
 return difficulty;
 }
 public void setDifficulty(Integer difficulty) {
 this.difficulty = difficulty;
 }
 public Integer getCookingtime() {
 return cooktime;
 }
 public void setCookingtimeime(Integer cooktime) {
 this.cooktime = cooktime;
 }
 public Integer getPreparationtime() {
 return preptime;
 }
 public void setPreparationtime(Integer preptime) {
 this.preptime = preptime;
 }
}
Main Controller:
@PutMapping("/recipes/edit/{id}")
 void updateRecipe2(@PathVariable int id, @RequestBody Recipe recipe ) {
 Recipe recipe_ = recipeRepository.findById(id).get();
 recipe_.setTitle(recipe.getTitle());
 System.out.println("sss " + recipe.getname());
 System.out.println("change");
 recipeRepository.save(recipe_);
 }

service.ts:

updateRecipe2 (id: number, recipe: any): Observable<any > {
 const url = `${this.usersUrl}/edit/${id}`;
 return this.http.put(url ,recipe);
}

where the updateRecipe2 gets called:

 save(): void {
 const id = +this.route.snapshot.paramMap.get('name');
 this.recipeService.updateRecipe2(id, this.recipes)
 .subscribe(() => this.gotoUserList());
 }

as soon as the user clicks save this functions saves the changes made. I hope the code snippets that I provided are enough to help solve the problem. Thank you in advance.

I am building a rest api with spring boot and I am using angularjs as it's frontend. I am pretty new to web-development.

asked Apr 2, 2020 at 17:54

2 Answers 2

2

You are sending a list of recipes to an api endpoint that expects a single recipe object.

Your options are:

  • Send only one recipe object at a time, for example:

    this.recipeService.updateRecipe2(id, this.recipes[0])
    
  • OR: create a new API endpoint to accept a list of recipes, to edit them in "batch"

    @PutMapping("/recipes/edit")
    void updateRecipes(@RequestBody List<Recipe> recipe ) {
    
answered Apr 2, 2020 at 18:02
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for the reply. I changed the recipeservice as you described but I dont understand the second point. How should I modify the api endpoint to accept a list of recipes?
I also got another error threw exception [Request processing failed; nested exception is java.util.NoSuchElementException: No value present] with root cause
Given that this API endpoint has ID as a URL path parameter, it's not easy to modify it, you would have to create a new API endpoint for this usage. The new endpoint would accept a list of recipes and do the "edit" operation for each recipe. As for the new error, it sounds like no recipe exists with the ID you are sending, which makes recipeRepository.findById(id).get() fail.
Thank you. I will try and modify the code. However I don't understand the error. when I check in the database if the values are present they are there.
That's always possible
|
0

my Example:

Use: @PostMapping

Code:

public void setTransacciones(List<Transacciones> transacciones) {
 this.transacciones = transacciones;
}

CodeBean:

public class Transacciones {
 public String getText() {
 return text;
 }
 public void setText(String text) {
 this.text = text;
 }
 private String text; 
}

Post(raw):

{
 "transacciones" : [ {"text" : "1"}, {"text" : "2"} ]
}

Result:

{
 "transacciones": [
 {
 "transaccionId": 2,
 "text": "1"
 },
 {
 "transaccionId": 3,
 "text": "2"
 }
 ]
}

BINGO!!

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.