拦截 JAX-RS web 服务请求添加 JSON 字段

Intercept JAX-RS web service request to add JSON field

我有一个 JAX-RS Web 服务,returns a 采用 JSON 请求参数(映射到参数对象),如下所示(在 WebLogic 12.2 中是 运行。 1).是否可以编写一个拦截器或过滤器,以便在调用 web 服务时,它会在 JSON 请求消息中添加一个额外的字段,以便下面的方法将在 requestParameters 中获取该额外字段?

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("LogIn")
public Response logIn(@Context HttpServletRequest request, Parameters requestParameters) {...}

谢谢!

拦截器

它可以通过 拦截器 来实现。

拦截器旨在通过操纵实体 input/output 流来操纵实体。有两种拦截器,ReaderInterceptor and WriterInterceptor

Reader 拦截器用于操作入站实体流。这些是来自 "wire" 的流。因此,使用 reader 拦截器,您可以在服务器端操纵请求实体流。编写器拦截器用于将实体写入 "wire" 的情况,这在服务器上意味着写出响应实体时

以下拦截器实现了 ReaderInterceptor 接口,允许您在服务器端修改请求的实体:

@Provider
public class CustomReaderInterceptor implements ReaderInterceptor {

    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) 
                      throws IOException, WebApplicationException {

        InputStream stream = context.getInputStream();

        // Manipulate the HTTP entity using the InputStream

        context.setInputStream(stream);
        return context.proceed();
    }
}

请注意上面的拦截器是全局的,也就是说,它会为所有的资源方法执行。

当使用 Jackson, your ReaderInterceptor#aroundReadFrom(ReaderInterceptorContext) 方法实现时可能是这样的:

// Create a Jackson ObjectMapper instance (it can be injected instead)
ObjectMapper mapper = new ObjectMapper();

// Parse the requested entity into a JSON tree
JsonNode tree = mapper.readTree(context.getInputStream());

// Add a property to the JSON
((ObjectNode) tree).put("field", "value");

// Set the input stream containing the manipulated JSON
context.setInputStream(new ByteArrayInputStream(mapper.writeValueAsBytes(tree)));

// Proceed to the next interceptor in the chain
context.proceed();

名称绑定

要仅对某些精选资源方法执行拦截器,您可以使用名称绑定

名称绑定 是一个概念,它允许对 JAX-RS 运行时说特定过滤器或拦截器将仅针对特定资源方法执行。当过滤器或拦截器仅限于特定资源方法时,我们说它是 name-bound.

可以使用 @NameBinding 注释将过滤器分配给资源方法。该注释用作其他用户实现的注释的元注释,这些注释应用于提供程序和资源方法。

名称绑定注解可以定义如下(注解的名称由你决定):

@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface CustomizeResponse { }

将上面定义的注解放在你的拦截器上 class:

@Provider
@CustomizeResponse
public class CustomReaderInterceptor implements ReaderInterceptor {
    ...
}

要将拦截器分配给资源方法,请将上面定义的注释放在资源方法上:

@GET
@CustomizeResponse
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
    ...
}

名称绑定也可以应用于资源 classes。这意味着拦截器将针对该资源的所有资源方法执行 class:

@Path("/foo")
@CustomizeResponse
public class MyResource() {
    ...
}

请注意,全局过滤器和拦截器总是被执行,所以即使对于具有任何名称绑定注释的资源方法也是如此。

其他资源

有关拦截器的更多详细信息,请查看 Jersey documentation