java访问用于JAXRS资源方法的默认JAXB封送器
我有许多REST资源类,它们返回一个模型实体,并依赖JAXR自动转换为XML(无需我自己的自定义提供程序)。我希望能够访问用于此目的的JAXB marshaller实例,以便配置ValidationEventHandler来捕获异常。我该怎么做
以下是我的示例实体资源:
@Path("/device")
public class DeviceResource extends CaBridgeServletResourceManager {
/**
* Get the server status.
*/
@GET
@Path("/config")
public DeviceConfigurationResponse getDeviceConfigurationResponse() {
DeviceService service = new DeviceService(getSessionContext());
DeviceConfigurationResponse response = service.createConfigurationResponse(getDeviceCredential());
return response;
}
}
我希望能够做到以下几点:
Marshaller marshaller = ... get jaxrs default marshaller ...
marshaller.setEventHandler(new MyMarshallerEventHandler());
如何获取jaxrs使用的默认封送拆收器?或者,对于我的资源类(上面)的每个实例,是否有一个新的marshaller实例可以访问
我宁愿避免为我拥有的每个实体类创建自定义提供程序类
# 1 楼答案
定义一个ContextResolver,它将被使用:
同样的事情也发生在破译者身上。我们通常实例化一次JAXBContext,并将其存储在provider类的静态成员中