如何为List< Some>?测试MessageBodyWriter? [英] How can I test a MessageBodyWriter for a List<Some>?
问题描述
我有一个JAX-RS资源方法。
@GET
@ Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public List< Some> list(){
final List< Some> list = get();
//列表,每个元素都可以。
返回清单;
}
问题在于 application / xml
生成500而没有任何特定服务器(tomcat)日志。
application / json
工作正常。
我检查列表
中每个元素的JAXB-marshaller。
我该如何调试?如何测试任何 MessageBodyWriter
列表< Some>
?
更新
此问题的根本原因(500没有错误日志)是一个错误的JAXB注释类。
我创建了一个 ExceptionMapper< Exception>
作为@peeskillet建议。
< pre class =lang-java prettyprint-override>
@Provider
public class MyExceptionMapper实现ExceptionMapper< Exception> {
@Override
public Response toResponse(final Exception exception){
exception.printStackTrace(System.err);
返回Response.serverError()。build();
}
}
然后我可以看到JAXB犯了什么错误。我仍然不明白为什么没有报告任何JAXB错误。
怎么能我调试了这个?
有时候,如果没有记录错误/异常,我会创建一个
ExceptionMapper< Throwable或Exception>
并打印堆栈跟踪。如果在JAX-RS上下文中抛出异常,它应该通过映射器。
I have a JAX-RS resource method.
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<Some> list() {
final List<Some> list = get();
// list and each elements are ok.
return list;
}
The problem is that application/xml
generates an 500 without any specific server(tomcat) log.
application/json
works fine.
I check JAXB-marshaller for every element in list
.
How can I debug this? How can I test any MessageBodyWriter
for List<Some>
?
UPDATE
The root cause of this problem (500 without a error log) is a wrongly JAXB-annotated class.
I created an ExceptionMapper<Exception>
as @peeskillet suggested.
@Provider
public class MyExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(final Exception exception) {
exception.printStackTrace(System.err);
return Response.serverError().build();
}
}
Then I could see what error JAXB made. I still don't understand why any JAXB-error is not reported.
"How can I debug this?"
Sometimes when errors/exceptions aren't being logged, I'll create an
ExceptionMapper<Throwable or Exception>
and just print the stack trace. If the exception is being thrown in the JAX-RS context, it should go through the mapper.
这篇关于如何为List< Some>?测试MessageBodyWriter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!