Jersey: Pass value from ContainerRequestFilter to endpoint

╄→尐↘猪︶ㄣ 提交于 2019-12-07 17:14:06

问题


I am using Jersey 2.9 and I have created a filter which will take an encrypted header value, and decipher it and then pass it along to the endpoint which was called on. I have no idea of how to do this, and I have been searching on the internet but not really found a concrete example of what I want to do. The filter is called, I just have issues passing a value from it to the endpoint.

Could you guys help me!

Here is some sample code:

public class MyFilter implements ContainerRequestFilter
{

    @Override
    public void filter(ContainerRequestContext requestContext) throws WebApplicationException {

        String EncryptedString = requestContext.getHeaderString("Authentication");

        /* Doing some methods to remove encryption */

        /* Get a string which I want to pass to the endpoint which was called on, in this example: localhost:4883/rest/test */
    }
}

@Path("rest")
public class restTest
{

    @Path("test")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String Testing(){

        /* Process the value from the MyFilter */
    }
}

回答1:


You can easily modify the header or add another one:

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    requestContext.getHeaders().add("X-Authentication-decrypted", decryptedValue);
}

This value can be injected in your resource-method:

@GET
@Produces(MediaType.APPLICATION_JSON)
public String Testing(@HeaderParam("X-Authentication-decrypted") String auth) {

}


来源:https://stackoverflow.com/questions/24350019/jersey-pass-value-from-containerrequestfilter-to-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!