将文档流发送到 Jersey @POST 端点

2022-01-21 00:00:00 rest streaming java jersey

我希望能够发送一堆文档流到网络服务.这将节省 Http 请求/响应开销并专注于文档本身.

I want to be able to send a stream of a bunch of documents to a web service. This will save on Http request/response overhead and focus on the documents themselves.

在python中你可以这样做:

In python you can do something like this:

r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
    data={'track': 'requests'}, auth=('username', 'password'),
    stream=True)

for line in r.iter_lines():
    if line: # filter out keep-alive new lines
        print json.loads(line)

我正在寻找一个将请求流式传输到 Jersey rest api 的示例.我希望看到客户端和服务器端显示它工作.但是我很难找到一个例子.

I'm looking for an example of someone streaming a Request to a Jersey rest api. I was hoping to see the client side and the server side to show it working. But i'm struggling hard to find an example out there.

该示例理想情况下会显示:

The example Ideally would show:

Client:
  Open request
  Iterate over huge document list
    Write document to open request stream
  Close request

Server:
  @POST method
    Open entity stream
    Iterate over entity stream while next document is available
        Process document
    Close entity stream              

如果我们做对了,您将在服务器上处理实体,同时仍在客户端上发送它们!大获全胜!

If we get it right you'll be processing entities on the Server while still sending them on the Client! Huge win!

推荐答案

实现这一点的最简单方法之一是让 Jersey 为 POST 处理程序提供一个用于 HTTP POST 正文的 InputStream.该方法可以使用您选择的 InputStream 和 JSON 解析器来解析然后处理每个对象.

One of the simplest ways to accomplish this is to let Jersey provide the POST handler with an InputStream for the HTTP POST body. The method can use the InputStream and JSON parser of your choice to parse then handle each object.

在下面的示例中,Jackson ObjectReader 生成一个 MappingIterator,它在传递数组中的每个 Person 文档时对其进行解析和处理到服务器

In the following example, the a Jackson ObjectReader produces a MappingIterator which parses and processes each Person document in the array as it is delivered to the server

/**
 * Parse and process an arbitrarily large JSON array of Person documents
 */
@Path("persons")
public static class PersonResource {

    private static final ObjectReader reader = new ObjectMapper().readerFor(Person.class);

    @Path("inputstream")
    @Consumes("application/json")
    @POST
    public void inputstream(final InputStream is) throws IOException {
        final MappingIterator<Person> persons = reader.readValues(is);
        while (persons.hasNext()) {
            final Person person = persons.next();
            // process
            System.out.println(person);
        }
    }
}

同样,Jersey 客户端框架可以在配置了 Jackson ObjectMapper 时发送文档流.以下示例使用 Jersey 测试框架演示了这一点.客户端流式传输任意大的 Person 文档

Likewise, the Jersey client framework can send a stream of documents when configured with a Jackson ObjectMapper. The following example demonstrates this with the Jersey Test framework. The client streams an arbitrarily large iterator of Person documents

public class JacksonStreamingTest extends JerseyTest {

    @Override
    protected Application configure() {
        return new ResourceConfig(PersonResource.class, ObjectMapperProvider.class);
    }

    /**
     * Registers the application {@link ObjectMapper} as the JAX-RS provider for application/json
     */
    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    public static class ObjectMapperProvider implements ContextResolver<ObjectMapper> {

        private static final ObjectMapper mapper = new ObjectMapper();

        public ObjectMapper getContext(final Class<?> objectType) {
            return mapper;
        }
    }

    @Override
    protected void configureClient(final ClientConfig config) {
        config.register(ObjectMapperProvider.class);
    }

    @Test
    public void test() {
        final Set<Person> persons = Collections.singleton(Person.of("Tracy", "Jordan"));
        final Response response = target("persons/inputstream").request().post(Entity.json(persons.iterator()));
        assertThat(response.getStatus()).isEqualTo(204);
    }
}

相关文章