我已经使用java hibernate jersery Framework制作了Restful API。

我必须发布已经完成的数据,但是缺少MealTypeName列之一。
这是我的DAO课程:

public class MealTypeDAO {

public void addMealType( MealType bean) {
    Session session = SessionUtil.getSession();
    Transaction tx = session.beginTransaction();
    addMealType(session, bean);
    tx.commit();
    session.close();
}

private void addMealType(Session session, MealType bean){
    MealType mealType = new MealType();
    mealType.setMealTypename(bean.getMealTypename());
    mealType.setModifiedon(bean.getModifiedon());
    mealType.setModifiedby(bean.getModifiedby());
    session.save(mealType);
}


这是我的资源类:

public class MealTypeResource {

@POST
@Path("/create")
@Consumes("application/json")
public Response addMealType(MealType meal){
    meal.setMealTypename(meal.getMealTypename());
    meal.setModifiedon(meal.getModifiedon());
    meal.setModifiedby(meal.getModifiedby());

    MealTypeDAO dao = new MealTypeDAO();
    dao.addMealType(meal);
    return Response.ok().build();
}

@GET
@Produces("application/json")
public Response getMealType() {
    MealTypeDAO dao = new MealTypeDAO();
    List mealTypes = dao.getMealType();
    String json = new Gson().toJson(mealTypes);
    return Response.ok().entity(json.toString()).build();
}


这是我的实体类:

public class MealType {


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int MealTypeId;
@Column
private String MealTypename;
@Column
private int modifiedby;
@Column
private String modifiedon;


public int getMealTypeId() {
    return MealTypeId;
}

public void setMealTypeId(int mealTypeId) {
    MealTypeId = mealTypeId;
}

public String getMealTypename() {
    return MealTypename;
}

public void setMealTypename(String mealTypename) {
    MealTypename = mealTypename;
}

public int getModifiedby() {
    return modifiedby;
}

public void setModifiedby(int modifiedby) {
    this.modifiedby = modifiedby;
}

public String getModifiedon() {
    return modifiedon;
}

public void setModifiedon(String modifiedon) {
    this.modifiedon = modifiedon;
}


MySQL数据库:

CREATE TABLE `mealtype`(`Mealtypeid` int(11) NOT NULL AUTO_INCREMENT,`MealTypename` varchar(20) DEFAULT NULL,`modifiedby` int(11) NOT NULL,`modifiedon` datetime NOT NULL,PRIMARY KEY (`Mealtypeid`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;


现在M从POSTMAN将这些数据发布到JSON FORMAT中:

{"MealTypeId":14,"MealTypename":"adsdf","modifiedby":1,"modifiedon":"2000-01-01 00:00:00"}


然后M获得这些数据:

{"MealTypeId":14,"modifiedby":1,"modifiedon":"2000-01-01 00:00:00"}


MealTypename丢失。为何如此?有人可以帮我吗?

最佳答案

您使用的名称的首字母大写MealTypename-这是一个原因。

名称为getMealTypename的吸气剂用于JSON属性mealTypename(不是MealTypename):

public String getMealTypename() {
    return MealTypename;
}


您需要指定一个JSON属性名称:

@JsonProperty("MealTypename")-杰克逊

@SerializedName("MealTypename") —对于Gson

您需要将此注释放入映射到JSON(MealType)的类的字段或getter。

并使用标准的Java命名约定。

public class MealType {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int mealTypeId;

@Column
private String mealTypename;

}


这看起来真的很奇怪:

meal.setMealTypename(meal.getMealTypename());
meal.setModifiedon(meal.getModifiedon());
meal.setModifiedby(meal.getModifiedby());

08-04 21:21