【问题标题】:JSON parse error: Cannot deserialize instance of.. out of START_ARRAY tokenJSON 解析错误:无法反序列化 .. 的实例超出 START_ARRAY 令牌
【发布时间】:2020-07-14 19:13:39
【问题描述】:

我知道有关此问题的 stackoverflow 上有几个问题。但我一直在花费数小时试图解决此错误,但没有任何成功。

我正在使用 mysql 数据库来存储值。

我不断收到来自 com.example.springboot.Recipe 文件。

这是 springboot 配方文件

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);
}

updateRecipe2 的调用位置:

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

   }

只要用户单击保存,此功能就会保存所做的更改。 我希望我提供的代码 sn-ps 足以帮助解决问题。 提前谢谢你。

我正在使用 spring boot 构建一个 rest api,并且我使用 angularjs 作为它的前端。我对网络开发很陌生。

【问题讨论】:

    标签: java spring-boot api jackson


    【解决方案1】:

    您正在向需要单个配方对象的 api 端点发送配方列表。

    您的选择是:

    • 一次只发送一个配方对象,例如:

      this.recipeService.updateRecipe2(id, this.recipes[0])
      
    • 或者:创建一个新的 API 端点来接受配方列表,以“批量”编辑它们

      @PutMapping("/recipes/edit")
      void updateRecipes(@RequestBody List<Recipe> recipe ) {
      

    【讨论】:

    • 感谢您的回复。我按照您的描述更改了recipeservice,但我不明白第二点。我应该如何修改 api 端点以接受食谱列表?
    • 我还遇到了另一个错误threw exception [Request processing failed; nested exception is java.util.NoSuchElementException: No value present] with root cause
    • 鉴于此 API 端点具有 ID 作为 URL 路径参数,因此修改它并不容易,您必须为此创建一个新的 API 端点。新端点将接受配方列表并为每个配方执行“编辑”操作。至于新的错误,听起来您发送的 ID 不存在任何配方,这使得 recipeRepository.findById(id).get() 失败。
    • 谢谢。我会尝试修改代码。但是我不明白这个错误。当我在数据库中检查值是否存在时,它们就在那里。
    • 总有可能
    【解决方案2】:

    我的例子:

    使用: @PostMapping

    代码:

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

    代码豆:

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

    发布(原始):

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

    结果:

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

    宾果游戏!!

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 2014-04-16
      • 2020-04-18
      • 2019-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      相关资源
      最近更新 更多