【问题标题】:Spring Controller Rest Call FailureSpring Controller Rest 调用失败
【发布时间】:2015-03-09 07:18:46
【问题描述】:

我正在对 Spring Controller 进行休息调用,它未能通过 BAD 请求(400 原因,原始问题 Spring Controller 400 (Bad Request)

我最初的休息调用参数是:

categoty: "Game"
itemDescription: "adas"
itemDiscount: 1
itemName: "asdas"
itemPrice: 1
itemQuantity: 1

这失败了,但是当我更改如下参数时,它起作用了:

categoty: "Game"
description: "test1"
discount: 10
name: "Test"
price: 10
quantity: 10

我的 Persistence POJO 课程是:

import javax.persistence.*;  

@Entity  
@Table(name= "Item")
public class Item {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private int id; 
    private String itemName,itemDescription,categoty;   
    private double itemPrice;
    private float itemDiscount;
    private int itemQuantity;


    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  

    public int getQuantity() {  
        return itemQuantity;  
    }  
    public void setQuantity(int quantity) {  
        this.itemQuantity = quantity;  
    }  

    public float getDiscount() {  
        return itemDiscount;  
    }  
    public void setDiscount(float discount) {  
        this.itemDiscount = discount;  
    }  

    public double getPrice() {  
        return itemPrice;  
    }  
    public void setPrice(double price) {  
        this.itemPrice = price;  
    }  
    public String getName() {  
        return itemName;  
    }  
    public void setName(String iName) {  
        this.itemName = iName;  
    }  
    public String getDescription() {  
        return itemDescription;  
    }  
    public void setDescription(String desc) {  
        this.itemDescription = desc;  
    }  
    public String getcategoty() {  
        return categoty;  
    }  
    public void setcategoty(String cat) {  
        this.categoty = cat;  
    }  


}

有人可以帮我理解为什么第二种类型的参数有效,而第一种无效(理想情况下,第一种参数是一对一映射到持久性类中的属性)?

【问题讨论】:

  • getter 和 setter 是一样的,它从所有变量中删除了前缀名称,检查这些示例 example 1example 2
  • 它使用 getter/setter。

标签: java spring rest


【解决方案1】:

Spring 调用实体类的设置器。 (属性quantity --> setQuantity(String) - 它不会查看您的成员的名称(例如itemQuantity)。

但是:将 setter/getter 命名为像您的成员一样总是一个好主意。您应该重命名您的成员以匹配 getter 和 setter。这样就更清楚了。

除此之外,getcategotysetcategoty 应重命名为驼峰式 getCategorysetCategory。顺便说一句:categotycategory 的错字吗?

【讨论】:

    【解决方案2】:

    getter和setter也是一样,都是去掉了所有变量的前缀名,查看以下链接:

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2019-06-27
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      相关资源
      最近更新 更多