简体   繁体   English

从JAX-RS响应中检索字符串

[英]Retrieving String from JAX-RS Response

In short terms, I simplified the problem a lot . 简而言之,我将问题简化了很多 I am calling this code, and the response is received with status 200 (OK): 我正在调用此代码,并且收到状态为200(确定)的响应:

Receiver.java : Receiver.java

Response response = componentInstanceService.getResource(componentResourceType);

However, I don't know how can I retrieve the String contained in the body from this method: 但是,我不知道如何从此方法中检索主体中包含的字符串:

Sender.java: Sender.java:

  @Override
    public Response getResource(ComponentResourceType resourceType) {
        String path = getPath();
        return Response.ok(this.getClass().getResourceAsStream(path)).build();
    }

Please note that the communication between classes is working fine, as long as the Response is OK, however, how can I retrieve the String that Response contains? 请注意,只要Response正常,类之间的通信就可以正常工作,但是,如何检索Response包含的String

This is what I would like to do roughly: 这是我想大致执行的操作:

Receiver: 接收方:

String result = componentInstanceService.getResource(componentResourceType);

如果您想从身体中读取字符串,只需使用

String result = componentInstanceService.getResource(componentResourceType).readEntity(String.class);

The documentation for Response makes this pretty clear: Response文档清楚地表明了这一点:

static Response.ResponseBuilder ok(java.lang.Object entity)

Create a new ResponseBuilder that contains a representation. 创建一个包含表示形式的新ResponseBuilder。

And: 和:

abstract java.lang.Object getEntity()

Return the response entity. 返回响应实体。

In other words, the object you passed to Response.ok is the entity. 换句话说,您传递给Response.ok的对象是实体。 You can retrieve it with the Response's getEntity() method. 您可以使用Response的getEntity()方法检索它。

Obviously, you will need to cast it: 显然,您将需要强制转换:

Response response = componentInstanceService.getResource(componentResourceType);
InputStream dataSource = (InputStream) response.getEntity();

Then you can read the stream as text. 然后,您可以将流作为文本读取。 You haven't mentioned the charset of your text files, so I'll assume it's UTF-8: 您没有提到文本文件的字符集,因此我假设它是UTF-8:

String result;
try (Scanner scanner = new Scanner(dataSource, StandardCharsets.UTF_8)) {
    result = scanner.useDelimiter("\\z").next();
}

Update: 更新:

I suspected this might happen. 我怀疑这可能发生。 You are returning a raw InputStream, which has no information about what type of data it is. 您将返回一个原始InputStream,它没有有关数据类型的信息。

Change Sender.java to return a DataSource: 更改Sender.java以返回数据源:

@Override
public DataSource getResource(ComponentResourceType resourceType) {
    String path = getPath();
    return new URLDataSource(this.getClass().getResource(path));
}

This way, the JAX-RS service will not only return HTTP 200 OK, but will also return a Content-Type header corresponding to the intuited type of your file. 这样,JAX-RS服务不仅将返回HTTP 200 OK,而且还将返回与您的文件的直观类型相对应的Content-Type标头。

You should then be able to invoke the method with: 然后,您应该能够使用以下方法调用该方法:

DataSource dataSource = componentInstanceService.getResource(componentResourceType);

String result;
try (Scanner scanner = new Scanner(dataSource.getInputStream(), StandardCharsets.UTF_8)) {
    result = scanner.useDelimiter("\\z").next();
}

There actually is a more robust way to read a DataSource. 实际上,还有一种更健壮的方式来读取数据源。 You can wrap it in a DataHandler: 您可以将其包装在DataHandler中:

DataSource dataSource = componentInstanceService.getResource(componentResourceType);
DataHandler handler = new DataHandler(dataSource);

DataFlavor flavor = DataFlavor.selectBestTextFlavor(
    handler.getTransferDataFlavors());

if (flavor == null) {
    // This should never happen with text files.
    throw new IllegalArgumentException(
        "Data has no flavors capable of supplying text.");
}

String result;
try (Reader reader = flavor.getReaderForText(handler)) {
    StringBuilder s = new StringBuilder();
    int c;
    while ((c = reader.read()) >= 0) {
        s.append((char) c);
    }
    result = s.toString();
} catch (UnsupportedFlavorException e) {
    // Since we started with a flavor provided by the DataHandler,
    // we should never get here.
    throw new RuntimeException(e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM