Meo*_*ode 8 java rest web-services jersey
正如在如何使用Jersey拦截器来获取请求主体中所讨论的那样,我正在修改ContainerRequestFilter中的EntityInputStream.
public filter(ContainerRequest request){
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
try{
Readerwriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
// DO SOMETHING WITH BYTES HERE
request.setEntityInputStream(new ByteArrayInputStream(requestEntity));
}/// error handling code here
}
Run Code Online (Sandbox Code Playgroud)
但是,后来我无法弄清楚如何访问修改后的InputStream.我可以在资源中获取ServletContext,但我无法弄清楚如何获取我在过滤器中实际修改的对象ContainerRequest.
我可以这样做吗?当我尝试这个时,泽西岛无法启动:
@Post
@Path("/test")
public Response test(@Context ContainerRequest cr){
// blah blah
return....
}
Run Code Online (Sandbox Code Playgroud)
泽西岛错误:
方法public javax.ws.rs.core.Response example.TestController.test(com.sun.jersey.spi.container.ContainerRequest)缺少依赖性,用资源的POST注释,类example.TestController,不被认为是有效的资源方法.
我被困在一个旧版本的球衣,1.8,所以我不确定这是否是问题的一部分.
您需要做的就是接受 anInputStream作为资源方法中的实体主体。如果你想要就ByteArrayInputStream直接投吧。
@POST
public Response post(InputStream in) {
ByteArrayInputStream bin = (ByteArrayInputStream)in;
}
Run Code Online (Sandbox Code Playgroud)
如果您还不知道,Jersey 如何将请求流(对于请求主体)转换为 Java 类型(例如 JSON 到 POJO)是通过MessageBodyReaders. 您可以在JAX-RS Entity Providers阅读有关它们的更多信息。
Jersey 已经附带了一些用于轻松转换类型的标准读取器,例如 String。大多数内容类型都可以转换为字符串。同样,它也有一个需要处理的读者InputStream。这可能是最简单的转换,因为请求已经作为 传入InputStream,因此读者实际上需要做的就是返回原始流,这就是传递给我们的方法的内容。
如果我们查看实现InputStreamProvider,我们可以看到这就是实际发生的情况。简单地返回原始流。由于过滤器发生在读取器之前,因此读取器只需返回我们设置的流即可。
这是使用Jersey Test Framework 的完整示例
public class StreamFilterTest extends JerseyTest {
public static class InputStreamFilter implements ContainerRequestFilter {
@Override
public ContainerRequest filter(ContainerRequest request) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityInputStream();
ReaderWriter.writeTo(in, out);
byte[] requestBytes = out.toByteArray();
byte[] worldBytes = " World".getBytes(StandardCharsets.UTF_8);
byte[] newBytes = new byte[requestBytes.length + worldBytes.length];
System.arraycopy(requestBytes, 0, newBytes, 0, requestBytes.length);
System.arraycopy(worldBytes, 0, newBytes, requestBytes.length, worldBytes.length);
request.setEntityInputStream(new ByteArrayInputStream(newBytes));
} catch (IOException ex) {
Logger.getLogger(InputStreamFilter.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
return request;
}
}
@Path("stream")
public static class StreamResource {
@POST
public String post(InputStream in) throws Exception {
ByteArrayInputStream bin = (ByteArrayInputStream) in;
StringWriter writer = new StringWriter();
ReaderWriter.writeTo(new InputStreamReader(bin), writer);
return writer.toString();
}
}
public static class AppConfig extends DefaultResourceConfig {
public AppConfig() {
super(StreamResource.class);
getContainerRequestFilters().add(new InputStreamFilter());
}
}
@Override
public WebAppDescriptor configure() {
return new WebAppDescriptor.Builder()
.initParam(WebComponent.RESOURCE_CONFIG_CLASS,
AppConfig.class.getName())
.build();
}
@Test
public void should_return_hello_world() {
String response = resource().path("stream").post(String.class, "Hello");
assertEquals("Hello World", response);
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试依赖项
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly2</artifactId>
<version>1.17.1</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |