本文介绍了Playframework:覆盖BadRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Playframework 2.0(标量版本),并且我想在我的一个控制器中覆盖"BadRequest"方法.我的控制器扩展了一个特征:

I'm using Playframework 2.0 (scala version) and I want to override "BadRequest" method in one of my controllers.My controller extends a trait:

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.json.JsValue
import play.api.libs.json.JsObject

/*
 * Simple trait to factor common code used by all controllers...
 */
trait AbstractController extends Controller {
 // Personal useful methods
 // ...
 // Implementation of an override of BadRequest ?
}

我的控制器:

package controllers

import play.api._
import play.api.mvc._
import play.api.Play.current
import play.api.libs.json._
import play.api.libs.json._
import play.api.libs.concurrent.Promise

object MyController extends AbstractController
{

 def myFunc (s: String) = {
    BadRequest(s) // should return what I'll define in MY BadRequest implementation
  }
}

我想做的事情:实现BadRequest的新功能(即返回带有错误详细信息的JSON),仅 用于扩展AbstractController的控制器.播放文档建议在Global Object(扩展GlobalSettings)中执行此操作,并覆盖onBadRequest方法.但是我不希望这样的全局方法...仅在特定控制器中使用.

What I want to do: implement a new comportment of BadRequest (i.e. return a JSON with error details) only for controllers extending AbstractController.Play documentation suggest doing that in Global Object (extends GlobalSettings) and override onBadRequest method. But I do not want a so global method... only in specific controllers.

什么是最佳解决方案?

感谢您的帮助.格雷格

Thanks for your help.Greg

推荐答案

我真的看不到这里存在什么问题.仅仅覆盖它有什么问题?

I don't really see what the problem is here. What is wrong with just overriding it?

trait AbstractController extends Controller {
  override val BadRequest = ... 

}

这篇关于Playframework:覆盖BadRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 14:25