2016-04-11 6 views
1

モデルエンティティを返すJAXRSに依存するいくつかのRESTリソースクラスがあります。このために使用されるJAXB marshallerインスタンスにアクセスできるようにするため、例外をキャッチするValidationEventHandlerを構成できます。これはどうすればいいですか?ここでJAXRSリソースメソッドに使用されるデフォルトのJAXBマーシャーにアクセスします。

は私のサンプルエンティティリソースです:

@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で使用されるデフォルトのマーシャラーを得るのですか?または、私のリソースクラスの各インスタンス(上記)にアクセスできる新しいマーシャラーインスタンスがありますか?

私は、私が持っているすべてのエンティティクラスに対してカスタムプロバイダクラスを作成することをやめます。

答えて

0

はContextResolverを定義し、それが使用されます:

@Provider 
public class JaxbMarshallerProvider implements ContextResolver<Marshaller> { 
    @Override 
    public Marshaller getContext(Class<?> type) { 
    } 
} 

そしてUnmarshallerのために同じことを。一般的にJAXBContextを一度インスタンス化し、それをプロバイダクラスの静的メンバーに隠します。

+0

はい、それは私のトリックです!非常にロブありがとうございます! –

関連する問題