- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在尝试在MessageBodyWriter接口(interface)的writeTo实现方法中直接将字符串写入OutputStream。我想在 try catch block 内执行此操作,以便在捕获异常时发送消息。然而,当我调试程序时,我意识到字符串永远不会被写入OutputStream(大小= -1)。
代码看起来像这样:
public void writeTo(final Object entityObject, final Class<?> aClass, final Type type,
final Annotation[] annotations, final MediaType mediaType,
final MultivaluedMap<String, Object> stringObjectMultivaluedMap,
final OutputStream outputStream) throws IOException, WebApplicationException {
try{
throw new JAXBException("error");
}catch(JAXBException j){
outputStream.write("HI".getBytes());
outputStream.flush();
}
最佳答案
新答案
您可以利用从 MessageBodyWriter 中的 writeTo 方法抛出的 WebApplicationException。
public void writeTo(DataObject dataObject, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
try {
throw new JAXBException("error");
} catch(JAXBException e) {
Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("HI")
.type("text/plain")
.build();
throw new WebApplicationException(response);
}
}
原始答案
在我看来,您最好从 MessageBodyWriter 抛出 JAXBException,然后创建 ExceptionMapper 来记录问题:
@Provider
public class JAXBExceptionMapper implements ExceptionMapper<JAXBException> {
public Response toResponse(JAXBException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e.getMessage());
.type("text/plain").build();
}
}
这将允许您返回指示出现问题的响应代码。
关于java - JAXB 在 writeTo 方法中写入 OutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4855575/
本文整理了Java中okreplay.YamlTapeLoader.writeTo()方法的一些代码示例,展示了YamlTapeLoader.writeTo()的具体用法。这些代码示例主要来源于Git
我必须使用 MTOM 从 Web 服务下载文件。我不知道下面的实现细节。我这样做,这是可行的(响应是从Web服务返回的对象,数据是javax.activation.DataHandler类型,file
我的目标是: 从 S3 读取文件, 更改其元数据 再次推送到S3 AWS java SDK 不允许推送输出流。因此,我必须将 outputstream 从 step2 转换为 inputstream。
我尝试将一些外语的 unicode 字符串发送到 SOAPMessage.writeTo()但它并没有逃脱它。 当我尝试发送 XML 转义字符串 StringEscapeUtils.escapeXml
我使用 SOAPMessage.writeTo(OutputStream)记录网络服务消息。一个问题是它也写附件。它占用空间并且二进制附件不可读。有什么方法可以记录没有附件的消息,例如 wrapper
尝试将 XmlDocument 序列化为文件。 XmlDocument 相当大;然而,在调试器中,我可以看到 InnerXml 属性中包含所有 XML blob——它没有被截断。 这是将我的 XmlD
本文整理了Java中io.protostuff.YamlIOUtil.writeTo()方法的一些代码示例,展示了YamlIOUtil.writeTo()的具体用法。这些代码示例主要来源于Github
我的应用程序中的 SOAP 消息包含从文件创建的附件。当我多次调用 soapMessage.writeTo(System.out); 时,它会引发以下异常: SEVERE: SAAJ0540: Err
我正在尝试写电子邮件至 .eml使用 Java Api 编写文件。写入文件后,我需要使用 ' SHA1 验证文件是否已成功下载。 ' 算法。我尝试了以下方法。 1)我已将电子邮件消息写入.eml文件,
我正在尝试读取邮箱中的邮件,但是在将邮件内容解析为 MimeMessage 期间,某些特定的邮件使进程挂起,它没有显示任何错误消息,日志显示程序停止在 MimeMessage.writeTo(Outp
我正在尝试创建一个 soapMessage 以便稍后传递到以下代码片段中: SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFa
如果我使用 writeTo 方法将 Message 对象内容保存到文件中,是否可以从该文件内容重新创建该对象? 我知道 Message 比使用 writeTo 方法保存的信息有更多的信息,例如商店、文
我正在从 SOAP 消息中获取附件并在我的应用程序中使用它们。 我不会从原始消息中删除附件。之后,我尝试使用以下代码将原始 包含附件 (SwA) 的 SOAPMessage 转换为字符串: ByteA
我一直在尝试在MessageBodyWriter接口(interface)的writeTo实现方法中直接将字符串写入OutputStream。我想在 try catch block 内执行此操作,以便
Retrofit 2 RequestBody writeTo()方法调用了两次,我使用的代码如下: ProgressRequestBody requestVideoFile = new Progres
本文整理了Java中org.apache.cxf.helpers.XMLUtils.writeTo()方法的一些代码示例,展示了XMLUtils.writeTo()的具体用法。这些代码示例主要来源于G
我使用 Serilog 进行日志记录,并使用控制台编写器将日志发送到标准输出,然后由 Filebeat 提取并发送到 Elastic。记录器的配置包括以下内容: .WriteTo.Console(ou
我看过很多 NLog 的示例配置。我注意到一些记录器使用 appendTo 属性指定目标,而其他记录器使用 writeTo 属性。 我找不到任何说明何时使用一个而不是另一个的文档。而且我无法发现任何明
我正在使用 java 获取邮件消息并将该消息保存到文件中。当我去读回文件时,我得到一个 javax.mail.internet.AddressException 下面是我用来写消息的代码 Proper
我正在尝试调用一个 SOAP 服务,但是在我构建了我的 SOAPMessage 之后,如果我调用 SOAPMessage.writeTo(out)服务调用已成功完成,但当我省略它时它会失败。 我很确定
我是一名优秀的程序员,十分优秀!