val config = new NingAsynchttpClIEntConfigBuilder().build()val builder = new AsynchttpClIEntConfig.Builder(config)val clIEnt = new NingWSClIEnt(builder.build)
但是在2.5.x中,现在不推荐使用NingWSClIEnt – 而是应该使用AhcWSClIEnt.
不幸的是,我没有找到一个完整的例子来解释Play之外的AhcWsClIEnt的创建和使用.目前我跟这个:
import play.API.libs.ws.ahc.AhcWSClIEntimport akka.stream.ActorMaterializerimport akka.actor.ActorSystemimplicit val system = ActorSystem()implicit val materializer = ActorMaterializer()val ws = AhcWSClIEnt()val req = ws.url("http://example.com").get().map{ resp => resp.body}(system.dispatcher)
这是创建AhcWsClIEnt的正确方法吗?有没有一种方法可以在没有ActorSystem的情况下创建AhcWSClIEnt?
解决方法 您可能正在使用编译时依赖注入,否则您只需使用@Inject()(ws:WSClIEnt),对吧?文档中有一个示例: https://www.playframework.com/documentation/2.5.x/ScalaWS#using-wsclient
所以你可以在你的应用程序加载器中写这样的东西:
lazy val ws = { import com.typesafe.config.ConfigFactory import play.API._ import play.API.libs.ws._ import play.API.libs.ws.ahc.{AhcWSClIEnt,AhcWSClIEntConfig} import play.API.libs.ws.ahc.AhcConfigBuilder import org.asynchttpclIEnt.AsynchttpClIEntConfig val configuration = Configuration.reference ++ Configuration(ConfigFactory.parseString( """ |ws.followRedirects = true """.stripmargin)) val parser = new WSConfigParser(configuration,environment) val config = new AhcWSClIEntConfig(wsClIEntConfig = parser.parse()) val builder = new AhcConfigBuilder(config) val logging = new AsynchttpClIEntConfig.AdditionalChannelinitializer() { overrIDe def initChannel(channel: io.netty.channel.Channel): Unit = { channel.pipeline.addFirst("log",new io.netty.handler.logging.LoggingHandler("deBUG")) } } val ahcBuilder = builder.configure() ahcBuilder.sethttpAdditionalChannelinitializer(logging) val ahcConfig = ahcBuilder.build() new AhcWSClIEnt(ahcConfig)}applicationlifecycle.addStopHook(() => Future.successful(ws.close))
然后将ws注入您的控制器.我不是100%肯定这种方法,如果有一些Play大师可以验证这一点,我会很高兴.关于ActorSystem,您只需要获取用于解析Future的线程池.您也可以导入或注入默认执行上下文:play.API.libs.concurrent.Execution.Implicits.defaultContext.或者您可以使用自己的:隐式val wsContext:ExecutionContext = actorSystem.dispatchers.lookup(“contexts.your-special-ws-config”).
总结以上是内存溢出为你收集整理的web-services – 为2.5.x播放WS standalone全部内容,希望文章能够帮你解决web-services – 为2.5.x播放WS standalone所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)