将文档流发送到Jersey @POST端点 [英] Sending a stream of documents to a Jersey @POST endpoint

查看:159
本文介绍了将文档流发送到Jersey @POST端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够发送一堆文档流到Web服务。这将节省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.

在以下示例中,a 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 Test框架演示了这一点。客户端流式传输一个任意大的迭代器 Person documents

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);
    }
}

这篇关于将文档流发送到Jersey @POST端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆