1

我刚刚完成了一个非常简单的回显程序,将名称发送到服务器,然后将其发送回客户端。但问题是:

名称应首先使用 UTF-8 编码,例如%xy%xy%...

然后我们将它发送到服务器:myhost:9000/send/%xy%xy...

方法是这样的:

public static Result send(final String name)
{
...
}

如果数据正确,则可以成功解码,没有任何异常和错误。但有时,编码后的名称可能无效,例如%20%A. 结果,它无法解码并导致内部错误。该过程甚至无法进入“发送”方法。

我想知道当我输入 url `myhost:9000/send/%20%A,然后将结果(假设它是“错误名称”)发送回客户端时,如何捕获此错误?

例外情况是:

 Unexpected exception
IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern
 java.net.URLDecoder.decode(URLDecoder.java:187)
 play.api.mvc.PathBindable$$anon$9.bind(Binders.scala:365)
 play.api.mvc.PathBindable$$anon$9.bind(Binders.scala:364)
 play.core.Router$RouteParams$$anonfun$fromPath$1.apply(Router.scala:1036)
 play.core.Router$RouteParams$$anonfun$fromPath$1.apply(Router.scala:1036)
 scala.Option.map(Option.scala:133)
 play.core.Router$RouteParams.fromPath(Router.scala:1036)
 Routes$$anonfun$routes$1.apply(routes_routing.scala:179)
 Routes$$anonfun$routes$1.apply(routes_routing.scala:87)
 scala.PartialFunction$$anon$3.apply(PartialFunction.scala:67)
 scala.PartialFunction$$anon$3.apply(PartialFunction.scala:66)
 play.core.Router$Routes$class.handlerFor(Router.scala:1251)
 Routes$.handlerFor(routes_routing.scala:14)
 play.api.GlobalSettings$$anonfun$onRouteRequest$1$$anonfun$apply$1.apply(Global.scala:64)
 play.api.GlobalSettings$$anonfun$onRouteRequest$1$$anonfun$apply$1.apply(Global.scala:63)
 scala.Option.flatMap(Option.scala:146)
 play.api.GlobalSettings$$anonfun$onRouteRequest$1.apply(Global.scala:63)
 play.api.GlobalSettings$$anonfun$onRouteRequest$1.apply(Global.scala:63)
 scala.Option.flatMap(Option.scala:146)
 play.api.GlobalSettings$class.onRouteRequest(Global.scala:63)
 play.api.DefaultGlobal$.onRouteRequest(Global.scala:120)
 play.core.server.Server$$anonfun$sendHandler$1$1.apply(Server.scala:60)
 play.core.server.Server$$anonfun$sendHandler$1$1.apply(Server.scala:59)
 scala.Either$RightProjection.map(Either.scala:285)
 play.core.server.Server$class.sendHandler$1(Server.scala:59)
 play.core.server.Server$$anonfun$getHandlerFor$4.apply(Server.scala:89)
 play.core.server.Server$$anonfun$getHandlerFor$4.apply(Server.scala:88)
 scala.Either$RightProjection.flatMap(Either.scala:277)
 play.core.server.Server$class.getHandlerFor(Server.scala:88)
 play.core.server.NettyServer.getHandlerFor(NettyServer.scala:38)
 play.core.server.netty.PlayDefaultUpstreamHandler.messageReceived(PlayDefaultUpstreamHandler.scala:226)
 org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:563
 org.jboss.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791)
 org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296)
 org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:366)
 org.jboss.netty.handler.codec.replay.ReplayingDecoder.callDecode(ReplayingDecoder.java:568)
 org.jboss.netty.handler.codec.replay.ReplayingDecoder.messageReceived(ReplayingDecoder.java:437)
 org.jboss.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:75)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:563)
 org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
 org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
 org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
 org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:91)
 org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:373)
 org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:247)
 org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
 org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
 org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
 java.lang.Thread.run(Thread.java:679)
4

1 回答 1

0

您需要按照文档中的说明使用 BodyPraser

这将允许您手动验证编码并将正确的错误返回给用户。

于 2013-03-04T11:48:59.503 回答