Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:35:53+00:00 2026-06-13T03:35:53+00:00

I have this play framework 2 code (simplified): import formatters.json.IdeaTypeFormatter._ object IdeaTypes extends Controller

  • 0

I have this play framework 2 code (simplified):

import formatters.json.IdeaTypeFormatter._

object IdeaTypes extends Controller {

  def list = Action { request =>
    Ok(toJson(IdeaType.find(request.queryString)))
  }

  def show(id: Long) = Action {
    IdeaType.findById(id).map { ideatype =>
      Ok(toJson(ideatype))
    }.getOrElse(JsonNotFound("Type of idea with id %s not found".format(id)))
  }
}

IdeaType class extends Entity, and it’s companion object, IdeaType, extends EntityCompanion.

As you may expect, I have this kind of code in every controller, so I’d like to extract the basic behavior to a trait, something like this:

abstract class EntityController[A<:Entity] extends Controller {
  val companion: EntityCompanion
  val name = "entity"

  def list = Action { request =>
    Ok(toJson(companion.find(request.queryString)))
  }
  def show(id: Long) = Action {
    companion.findById(id).map { entity =>
      Ok(toJson(entity))
    }.getOrElse(JsonNotFound("%s with id %s not found".format(name, id)))
  }
}

But I get the following error:

[error] EntityController.scala:25: No Json deserializer found for type List[A]. 
[error] Try to implement an implicit Writes or Format for this type.
[error]     Ok(toJson(companion.find(request.queryString)))
[error]              ^
[error] EntityController.scala:34: No Json deserializer found for type A.
[error] Try to implement an implicit Writes or Format for this type.
[error]       Ok(toJson(entity))
[error]                ^

I don’t know how to tell that the implicit Writes will be implemented by the classes implementing the EntityController trait (or inheriting the abstract class EntityController)

— edit

so far now I’m doing it like this:

abstract class CrudController[A <: Entity](
  val model: EntityCompanion[A],
  val name: String,
  implicit val formatter: Format[A]
) extends Controller {

and use it like this

object CrudIdeaTypes extends CrudController[IdeaType](
  model = IdeaType, 
  name = "type of idea", 
  formatter = JsonIdeaTypeFormatter
)

I could’t get scala to automatically pick it using implicits. I tried with this import but it didn’t work

import formatters.json.IdeaTypeFormatter._
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:35:54+00:00Added an answer on June 13, 2026 at 3:35 am

    If you want the controler classes themselves to define the implicit, then just declare abstract implicit values, and define them in the derived classes.

    abstract class EntityController[A<:Entity] extends Controller {
      protected implicit def entityWriter: Writes[A]
      protected implicit def entityListWriter: Writes[List[A]]
      ...      
    }
    
    class MyEntity extends Entity {
      ...
    }
    
    class MyEntityController extends EntityController[MyEntity] {
      protected def entityWriter: Writes[MyEntity] = ...
      protected def entityListWriter: Writes[List[MyEntity]] = ...    
    }
    

    However, it is much more practical to define these implicits outside the controller, typically in the companion object of your entity, so that they the compiler can find them automatically without import.
    Then, pass the implicit values to the constructor of EntityController:

    abstract class EntityController[A<:Entity](implicit entityWriter: Writes[A], entityListWriter: Writes[List[A]] ) extends Controller {
      ...      
    }
    
    class MyEntity extends Entity {
      ...
    }
    object MyEntity {
      protected implicit def entityWriter: Writes[A]
      protected implicit def entityListWriter: Writes[List[A]]
    }
    
    class MyEntityController extends EntityController[MyEntity] {
      ...
    }
    

    Final note, the implicit to List[MyEntity] is probably unneeded( thus only the implicit for MyEntity would need to be explicitly defined). I have not checked, but usually when using this “typeclass pattern”,
    the framework will already define an implicit for each List[T], provided that there is an implicit for T. This is probably the case, though I have not checked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Controller defined like this: package controllers import play.api._ import play.api.mvc._ import
On a Play Framework 2 project if I have any javascript code with this
I'm using Play Framework, and have the following code: import org.hibernate.exception.ConstraintViolationException; ... public class
I have a Play! framework with two actions which contain redundant code. So I
I was building a class using play framework 1.2.x . I have a Controller
I have to use scala parser inside Play Framework application. import scala.tools.nsc._ trait Foo
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
I have to play sounds on GUI events, like clicking buttons etc. For this
I'm trying to create a plugin for Play Framework 2.0 (latest code in Github
I have installed jpagen module in my system in play framework. Which successfully generates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.