Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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 JAX-RS自定义头未添加到重定向响应_Java_Web Services_Redirect_Http Headers_Jax Rs - Fatal编程技术网

Java JAX-RS自定义头未添加到重定向响应

Java JAX-RS自定义头未添加到重定向响应,java,web-services,redirect,http-headers,jax-rs,Java,Web Services,Redirect,Http Headers,Jax Rs,我正在尝试向HTTP 303(重定向)响应添加一些自定义头参数。然而,新的标题似乎被从响应中删除了 此代码用于接收请求并返回HTTP 303响应: @POST @Path("/authorize") @Produces("application/x-www-form-urlencoded") public Response getOAuthGrant(@HeaderParam(OAuth2.AUTHORIZATION) @DefaultValue("") String authorizat

我正在尝试向HTTP 303(重定向)响应添加一些自定义头参数。然而,新的标题似乎被从响应中删除了

此代码用于接收请求并返回HTTP 303响应:

@POST
@Path("/authorize")
@Produces("application/x-www-form-urlencoded")
public Response getOAuthGrant(@HeaderParam(OAuth2.AUTHORIZATION)    @DefaultValue("") String authorization,
                              @HeaderParam(OAuth2.CLIENT_ID)        @DefaultValue("") String clientId,
                              @HeaderParam(OAuth2.CLIENT_SECRET)    @DefaultValue("") String clientSecret,
                              @HeaderParam(OAuth2.GRANT_TYPE)       @DefaultValue("") String grantType) throws InternalServerException, UnauthorizedException {

        OAuth2.validateGrantRequest(clientId, clientSecret, authorization, grantType);

        ApiTokenV2 apiTokenV2 = new ApiTokenV2();

        try {
            apiTokenV2 = TokenManager.getApiToken(clientId);

            if (apiTokenV2 != null) {
                apiTokenV2.generateAccessGrant(clientId);
            } else {
                logger.error("Error in TokenEndpoint. Retrieved token is null.");
                throw new InternalServerException("A server error occurred while trying to authorize the requester. Could not find 'generateAccessGrant' method");
            } 
        } catch (NamingException e) {
            throw new InternalServerException("A server error occurred while trying to authorize grant request. Could not find 'generateAccessGrant' method.", e);
        }

        return Response.status(Response.Status.SEE_OTHER)
                       .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE)
                       .header("Location", "/api/token")
                       .header("Authorization", "OAuth")
                       .header("Access-Grant", apiTokenV2.getAccessGrant())
                       .build();
}

我做错了什么?我应该改用
@Context
吗?

您尝试做的应该可以

@POST
@Path("/authorize")
public Response authorize() {
    return Response.status(Response.Status.SEE_OTHER)
            .header(HttpHeaders.LOCATION, "/api/token")
            .header("X-Foo", "bar")
            .build();
}
使用
curl执行它

% curl -v -X POST http://localhost:8080/WebApplication1/rest/console/authorize
* About to connect() to localhost port 8080 (#0)
*   Trying ::1... connected
> POST /WebApplication1/rest/console/authorize HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 303 See Other
< X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)
< Server: GlassFish Server Open Source Edition 3.1.2.2
< Location: /api/token
< X-Foo: bar
< Content-Length: 0
< Date: Wed, 07 Nov 2012 08:20:24 GMT
< 
* Connection #0 to host localhost left intact
* Closing connection #0
并设置感兴趣的标题:

Location: /api/token
X-Foo: bar

您可以使用
@生成
注释。您的方法生成什么响应实体?+1,尽管我建议使用
HttpHeaders.LOCATION
常量。
Location: /api/token
X-Foo: bar