use*_*403 4 json web-services jax-rs weblogic12c
我有一个JAX-RS Web服务,它返回一个获取JSON请求参数(映射到Parameter对象),如下所示(它在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) {...}
Run Code Online (Sandbox Code Playgroud)
谢谢!
它可以通过拦截器实现.
拦截器旨在通过操纵实体输入/输出流来操纵实体.有两种拦截器,ReaderInterceptor和WriterInterceptor.
读取器拦截器用于操纵入站实体流.这些是来自"电线"的流.因此,使用读取器拦截器,您可以在服务器端操纵请求实体流.Writer拦截器用于将实体写入"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();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,上面的拦截器是全局的,也就是说,它将针对所有资源方法执行.
使用Jackson时,您的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();
Run Code Online (Sandbox Code Playgroud)
要仅为某些手工挑选的资源方法执行拦截器,可以使用名称绑定.
名称绑定是一种概念,它允许向JAX-RS运行时说明仅针对特定资源方法执行特定过滤器或拦截器.当过滤器或拦截器仅限于特定的资源方法时,我们说它是名称绑定的.
可以使用@NameBinding注释将过滤器分配给资源方法.注释用作应用于提供者和资源方法的其他用户实现的注释的元注释.
名称绑定注释可以定义如下(注释的名称取决于您):
@NameBinding
@Retention(RUNTIME)
@Target({TYPE, METHOD})
public @interface CustomizeResponse { }
Run Code Online (Sandbox Code Playgroud)
将上面定义的注释放在拦截器类上:
@Provider
@CustomizeResponse
public class CustomReaderInterceptor implements ReaderInterceptor {
...
}
Run Code Online (Sandbox Code Playgroud)
要将拦截器分配给资源方法,请将以上定义的注释放在资源方法上:
@GET
@CustomizeResponse
@Produces(MediaType.APPLICATION_JSON)
public Response myMethod() {
...
}
Run Code Online (Sandbox Code Playgroud)
名称绑定也可以应用于资源类.这意味着将为该资源类的所有资源方法执行拦截器:
@Path("/foo")
@CustomizeResponse
public class MyResource() {
...
}
Run Code Online (Sandbox Code Playgroud)
请注意,始终执行全局过滤器和拦截器,因此即使对于具有任何名称绑定注释的资源方法也是如此.
有关拦截器的更多详细信息,请查看Jersey 文档.