You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AngularJS $http.post调用Spring Boot接口报错问题求助

Hey there, let's work through your issues step by step—none of them are related to JSON property order, so we can rule that out right away!

1. First Error: Missing Default Constructor

The first error Cannot construct instance of Task (no Creators, like default construct, exist) happens because Jackson (the JSON library Spring uses) needs a no-argument constructor to deserialize JSON into your Task object. Your original Task class only had a parameterized constructor, so Jackson couldn't create a new instance to populate with the JSON data.

You already fixed this by adding a default constructor—great call! That's why you moved to the second error.

2. Second Error: Type Mismatch for task_category

The new error Cannot deserialize value of type int from String "OOKK" tells us exactly what's wrong: your frontend is sending a string value for task_category, but your Task entity expects an int.

Here's how to fix this, plus a couple of best practices:

Fix 1: Restrict Frontend Input to Numbers

Change your HTML input for category from type="text" to type="number". This ensures users can only enter numeric values, and Angular will bind the input as a number (not a string) to task.task_category:

<input type="number" id="category" ng-model="task.task_category">

Fix 2: Correct Angular's $http.post Usage

You don't need to manually call JSON.stringify(data)—Angular's $http.post automatically serializes the data to JSON and sets the correct Content-Type: application/json header. Removing the stringify will ensure the data is sent properly:

$scope.saveNewTask= function(data) {
  $http.post("/Tasks", data)
  .then(function(response){
    if(response.data) console.log("Operation Successful");
  })
}

Fix 3: (Optional) Clean Up Your Task Entity

While public fields work, it's better practice to use private fields with getters and setters. This also ensures Jackson can properly access and set values (though it works with public fields too, this is more maintainable):

@Entity
@Table(name="ts_tasks")
public class Task {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;
  
  @Column(name="name")
  private String name;
  
  @Column(name="task_category")
  private int task_category;

  // Required default constructor for Jackson
  public Task() {}

  public Task(String name, int task_category) {
    this.name = name;
    this.task_category = task_category;
  }

  // Getters and Setters
  public int getId() { return id; }
  public void setId(int id) { this.id = id; }
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
  public int getTask_category() { return task_category; }
  public void setTask_category(int task_category) { this.task_category = task_category; }
}

Final Notes

  • JSON property order has no impact on Jackson's deserialization—you don't need to worry about where id appears in the JSON.
  • Make sure your frontend's task object matches the structure of the Task entity (which it does now, with name and task_category).

Once you apply these fixes, your post request should work as expected!

内容的提问来源于stack exchange,提问作者Dean Strydom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:24:18