Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在操作中检索/更改url参数_Java_Http_Playframework_Playframework 2.0_Playframework 2.1 - Fatal编程技术网

Java 在操作中检索/更改url参数

Java 在操作中检索/更改url参数,java,http,playframework,playframework-2.0,playframework-2.1,Java,Http,Playframework,Playframework 2.0,Playframework 2.1,我从前端调用userPrivateProfile控制器。路由是/api/user/private/:id,所以假设我在/api/user/private/65处调用。在我执行控制器之前,请求由SecurityAuthAction接收,其中我确保请求头具有令牌,如果是这种情况,我希望将:id更改为其他内容 Controller.java @With(SecurityAuthAction.class) public Result userPrivateProfile(Long i

我从前端调用userPrivateProfile控制器。路由是/api/user/private/:id,所以假设我在/api/user/private/65处调用。在我执行控制器之前,请求由SecurityAuthAction接收,其中我确保请求头具有令牌,如果是这种情况,我希望将:id更改为其他内容

Controller.java

  @With(SecurityAuthAction.class)
        public Result userPrivateProfile(Long id) {
            //LOGIC
        }
public Promise<SimpleResult> call(Http.Context ctx) throws Throwable {
        String[] authTokenHeaderValues = ctx.request().headers()
                .get(AUTH_TOKEN_HEADER);
       
        if ((authTokenHeaderValues != null) && (authTokenHeaderValues.length == 1) && (authTokenHeaderValues[0] != null)) {
            Long userId = sessionService
                    .findUserByToken(authTokenHeaderValues[0]);
            ctx.args.put("id",userId.toString());
            
        return delegate.call(ctx);
    }
SecurityAuthAction.java

  @With(SecurityAuthAction.class)
        public Result userPrivateProfile(Long id) {
            //LOGIC
        }
public Promise<SimpleResult> call(Http.Context ctx) throws Throwable {
        String[] authTokenHeaderValues = ctx.request().headers()
                .get(AUTH_TOKEN_HEADER);
       
        if ((authTokenHeaderValues != null) && (authTokenHeaderValues.length == 1) && (authTokenHeaderValues[0] != null)) {
            Long userId = sessionService
                    .findUserByToken(authTokenHeaderValues[0]);
            ctx.args.put("id",userId.toString());
            
        return delegate.call(ctx);
    }
公共承诺调用(Http.Context ctx)抛出可丢弃的{
字符串[]authTokenHeaderValues=ctx.request().headers()
.get(身份验证令牌头);
如果((authTokenHeaderValues!=null)&&&(authTokenHeaderValues.length==1)&&(authTokenHeaderValues[0]!=null)){
长userId=sessionService
.findUserByToken(authTokenHeaderValue[0]);
ctx.args.put(“id”,userId.toString());
返回委托呼叫(ctx);
}
我的问题是

  • 无法使用ctx从原始调用中检索指定的:id

  • 因为我找不到请求参数的位置,所以我也无法更改它

  • 我尝试遍历ctx.args映射,但在那里找不到任何内容。输出为:

    动词路线_

    行动法

    路由控制器

    路线图注释

    路线图

    得到

    用户私有配置文件

    控制器,控制器

    /api/user/private/$id


    感谢您的帮助:)

    不幸的是,Play框架(当然是2.1版)在执行操作组合时,您无法轻松访问URL查询参数。您可能对此感兴趣。其中提到的一个解决方法是在
    SecurityAuthAction
    中解析URL,以获取
    id
    查询参数的值。但是,这有点混乱,无法帮助您解决问题的下一部分,它在到达下游操作之前更改id

    在服务器处理请求时更改请求的详细信息对我来说似乎不常见,而且是错误的。通常,如果您想更改客户端请求的内容,您会发出HTTP 303响应,将它们重定向到您希望它们访问的URL。但这并不是重定向的情况。我认为您应该做的只是更改h您对
    sessionService
    的调用向下转到主控制器类:

    SecurityAuthAction.java 如果整个应用程序都需要
    userId
    ,那么它可能是应用程序cookie中的候选项