Java Reference
In-Depth Information
while
while ( true
true ) {
Report report = generateReport . invoke ( Report . class );
renderReport ( report );
Thread . sleep ( 300000 );
}
The example code prebuilds a GET Invocation that will fetch a JSON report summary of
orders made in the last five minutes. We then loop every five minutes and reexecute the in-
vocation. Sure, this example is a little bit contrived, but I think you get the idea.
Exception Handling
One thing we didn't discuss is what happens if an error occurs when you use an invocation
style that automatically unmarshalls the response. Consider this example:
Customer customer = client . target ( "http://commerce.com/customers/123" )
. accept ( "application/json" )
. get ( Customer . class );
In this scenario, the client framework converts any HTTP error code into one of the excep-
tion hierarchy exceptions discussed in Exception Hierarchy . You can then catch these excep-
tions in your code to handle them appropriately:
try
try {
Customer customer = client . target ( "http://commerce.com/customers/123" )
. accept ( "application/json" )
. get ( Customer . class );
} catch
catch ( NotAcceptableException notAcceptable ) {
...
} catch
catch ( NotFoundException notFound ) {
...
}
If the server responds with an HTTP error code not covered by a specific JAX-RS exception,
then a general-purpose exception is thrown. ClientErrorException covers any error code
in the 400s. ServerErrorException covers any error code in the 500s.
This invocation style will not automatically handle server redirects—that is, when the server
sends one of the HTTP 3xx redirection response codes. Instead, the JAX-RS Client API
throws a RedirectionException from which you can obtain the Location URL to do the
redirect yourself. For example:
WebTarget target = client . target ( "http://commerce.com/customers/123" );
boolean
boolean redirected = false
false ;
Customer customer = null
null ;
ddo {
Search WWH ::




Custom Search