类型列表采用类型参数(Play 中的编译错误)

Type List takes type parameters (Compilation Error in Play )

我目前正在使用 Play 学习 RESTFUL API 的基础知识,但遇到了一些问题:我正在学习一些长期教程,但认为正确的 Scala 语法失败了!需要帮助,谢谢 here is the screenshot of error

 package controllers

import play.api.libs.json.Json
import javax.inject.Inject
import play.api.Configuration
import play.api.mvc.{AbstractController, ControllerComponents}

import scala.concurrent.ExecutionContext


class PlacesController @Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder, ec: ExecutionContext, configuration: Configuration)
  extends AbstractController(cc) {


  case class PlacesController(id: Int, name: String)

  val thePlaces: List = List(
    thePlaces(1, "newyork"),
    thePlaces(2, "chicago"),
    thePlaces(3, "capetown")
  )

  implicit val thePlacesWrites = Json.writes[PlacesController]

  def listPlaces = Action {
    val json = Json.toJson(thePlaces)
    Ok(json)
  }}

你的代码有不少问题。您在定义 thePlaces 的同时在定义的右侧调用 thePlaces 本身。

另外,你的命名很混乱。

试试这个:

final case class Place(id: Int, name: String)

object Place {
  implicit val placeWrites = Json.writes[Place]
}

class PlacesController ... {

  val thePlaces: List[Place] = List(
    Place(1, "newyork"),
    Place(2, "chicago"),
    Place(3, "capetown")
  )

  def listPlaces = Action {
    val json = Json.toJson(thePlaces)
    Ok(json)
  }
}

终于找到答案了,希望对以后的其他人有所帮助!!

class PlacesController @Inject()(cc: ControllerComponents)(implicit assetsFinder: AssetsFinder, ec: ExecutionContext, configuration: Configuration)
  extends AbstractController(cc) {


  case class PlacesController(id: Int, name: String)

  val thePlaces: List[(Int, String)] = List(
    (1, "newyork"),
    (2, "chicago"),
    (3, "capetown")
  )

  implicit val thePlacesWrites = Json.writes[PlacesController]

  def listPlaces = Action {
    val json = Json.toJson(thePlaces)
    Ok(json)
  }


}