Java JSON控制器到Scala



我很难理解JSON文档。

我想把这个JSON结果转换成Scala,但我很迷茫。我已经尝试了很多东西,但我仍然在学习Scala,所以没有一个真正值得发布,因为我甚至不确定它是否有意义。

我在Scala中使用Anorm ORM。id是Pk[Long]

public static Result checkName(String clubname){
      ObjectNode jresult = Json.newObject();
     if (Club.clubExists(clubname)) {
         jresult.put("error", "Club Name Exists");
         return status(409, jresult); // 409 - Conflict
     } else {
         jresult.put("status", "OK");
         return ok(jresult);
     }
 }

clubExists在模型中:

public static boolean clubExists(String name) {
    Club club = find.where().eq("club_name", name).findUnique();
    return (club != null);
}

模型的其余部分非常基本:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "club_seq")
public Long clubId;
@Column(unique=true, length = 50)
public String clubName;
public Long creator;
public DateTime created;
public Club(String clubName, Long creator) {
    this.clubName = clubName;
    this.creator = creator;
    this.created = new DateTime();
}
public static Finder<Long, Club> find = new Finder<Long, Club>(Long.class, Club.class);
public static Club create(String name, Long creator) {
    Club club = new Club(name, creator);
    club.save();
    return club;
}
public static Result checkName(String clubname){
     ObjectNode jresult = Json.newObject();
     if (Club.clubExists(clubname)) {
         jresult.put("error", "Club Name Exists");
         return status(409, jresult); // 409 - Conflict
     } else {
         jresult.put("status", "OK");
         return ok(jresult);
     }
}

Scala中的是(添加as JSON以更改MIME类型(:

def checkName(clubName:String) = Action {
  val jresult = Json.obj()
  if (Club.exists(clubName)) {
    Conflict(jresult) as JSON
  } else {
    Ok(jresult) as JSON
  } 
}

最新更新